我正在制作一个应用程序,显示随机的表情包。我想做一个重新加载我的程序的按钮,这样就可以生成一个新的表情包。所以简而言之,你打开应用程序,看到表情包,如果你想看下一个表情包,你必须点击按钮,程序重新加载,一个新的表情包被生成……
这是我的代码..
#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()发布于 2021-05-03 04:52:21
尝试如下所示:
#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()https://stackoverflow.com/questions/67357354
复制相似问题