我想要一个按钮,在python kivy中重新启动我的程序



我正在制作一个显示随机模因的应用程序。我想制作一个按钮来重新加载我的程序,这样就可以生成一个新的模因。简而言之,你打开应用程序,看到表情包,如果你想查看下一个表情包,你必须点击按钮,程序就会重新加载,一个新的表情包就会被添加。。。。

这是我的密码。。

#First Import The Modules!!
import requests
import json
from bs4 import BeautifulSoup
import shutil
import urllib.request
from urllib.parse import urljoin
import time
import sys
from kivy import app
from kivy.app import App
from kivy.properties import ListProperty, ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivymd.utils import asynckivy

#build the class App
class Button_App(App):
def build(self):
#request the content from the url
response = requests.get("https://meme-api.herokuapp.com/gimme")
#extract the main meme url from json file and print it
pass_times = response.json()['url']
#identify the image from that meme url
image_url = pass_times
r = requests.get(image_url)
#convert the image variable (r) to string
r = str(requests.get(image_url))
#Let's start the making of UI!!
f1 = FloatLayout()
img = AsyncImage(source=pass_times)
img.size_hint_x = 1
img.size_hint_y = 1
btn = Button(text="Exit!",
font_size="35sp",
background_color=(0, 0, 1, 1),
color=(1, 1, 1, 1),
size=(32, 32),
size_hint=(.2, .1),
pos=(130, 50))
btn.bind(on_press=self.callback)
f1.add_widget(img)
f1.add_widget(btn)
# return the UI:
return f1
def callback(self, event):
exit()
root = Button_App()
root.run()

试试这样的东西:

#First Import The Modules!!
import requests
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.clock import Clock

#build the class App
class Button_App(App):
def build(self):
self.img = None
f1 = FloatLayout()
btn = Button(text="Exit!",
font_size="35sp",
background_color=(0, 0, 1, 1),
color=(1, 1, 1, 1),
size_hint=(.25, .1),
pos_hint={'x':0, 'y':0})
btn.bind(on_press=self.callback)
f1.add_widget(btn)
btn = Button(text='New Meme',
font_size="35sp",
background_color=(0, 0, 1, 1),
color=(1, 1, 1, 1),
size_hint=(.25, .1),
pos_hint={'right':1, 'y':0})
btn.bind(on_press=self.get_meme)
f1.add_widget(btn)
Clock.schedule_once(self.get_meme)
# return the UI:
return f1
def get_meme(self, *args):
if self.img:
self.root.remove_widget(self.img)
#request the content from the url
response = requests.get("https://meme-api.herokuapp.com/gimme")
#extract the main meme url from json file and print it
pass_times = response.json()['url']
#identify the image from that meme url
image_url = pass_times
r = requests.get(image_url)
#convert the image variable (r) to string
r = str(requests.get(image_url))
self.img = AsyncImage(source=pass_times)
self.img.size_hint_x = 1
self.img.size_hint_y = 1
self.root.add_widget(self.img, index=2)
def callback(self, event):
exit()

root = Button_App()
root.run()

最新更新