首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想要一个在python kivy中重新启动我的程序的按钮。

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

Stack Overflow用户
提问于 2021-05-02 22:14:33
回答 1查看 27关注 0票数 0

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

这是我的代码..

代码语言:javascript
复制
#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()
EN

回答 1

Stack Overflow用户

发布于 2021-05-03 04:52:21

尝试如下所示:

代码语言:javascript
复制
#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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67357354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档