【问题标题】:use of random.choice with kivy buttons使用带有 kivy 按钮的 random.choice
【发布时间】:2020-04-24 11:18:15
【问题描述】:

所以,我正在使用 kivy 创建一个程序,该程序依赖于能够通过使用按钮从已创建的字典中随机选择一个位置。我希望选择在窗口上显示按钮,而不是在命令行中。有没有办法做到这一点?我附上了 .py 代码的 sn-p 和我的 kivy 代码。我希望输出显示在窗口(还附有图片)上,上面写着“转到:”

.py 代码:

import kivy
import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
FoodPlaces={'Asian': ['joy yee','strings','ramen san','chi cafe']}
class MainWindow(Screen):
    pass
class FoodWindow(Screen):
    def asianBtn(self):
        print(random.choice(FoodPlaces['Asian']))
class AsianWindow(Screen):
    pass
class WindowManager(ScreenManager):
    pass

kv=Builder.load_file("picker.kv")


class pickerApp(App):
    def build(self):
        return kv

if __name__=="__main__":
    pickerApp().run()

kivy 代码:

WindowManager:
    MainWindow:
    FoodWindow:
    AsianWindow:
<MainWindow>:
    name:"main"
    GridLayout:
        cols:1
        Label:
            text:"Pick a Category"
        Button:
            text:"Food"
            on_release:
                app.root.current="food"
                root.manager.transition.direction="left"
<FoodWindow>:
    name: "food"
    GridLayout:
        cols:1
        Label:
            text:"Pick a Food Type"
        Button:
            text: "Asian"
            on_release:
                app.root.current="asian"
                root.manager.transition.direction="left"
                root.asianBtn()
        Button:
            text: "Go Back"
            on_release:
                app.root.current="main"
                root.manager.transition.direction="right"
<AsianWindow>
    name:"asian"
    GridLayout:
        cols:1
        Label:
            text: "Go to:"
        Button:
            text: "Go Back"
            on_release:
                app.root.current="food"
                root.manager.transition.direction="right"

【问题讨论】:

  • 你想显示什么样的输出?
  • @JohnAnderson 我想显示我的函数输出的文本或“字符串”

标签: python kivy


【解决方案1】:

一种方法是将id 添加到Label

<AsianWindow>
    name:"asian"
    GridLayout:
        cols:1
        Label:
            id: goto   # Use this id to access the Label
            text: "Go to:"
        Button:
            text: "Go Back"
            on_release:
                app.root.current="food"
                root.manager.transition.direction="right"

为了简单起见,将asianBtn() 方法放在AsianWindow 类中:

class AsianWindow(Screen):
    def asianBtn(self):
        self.ids.goto.text = random.choice(FoodPlaces['Asian'])

并将其在kv 中的调用更改为:

<FoodWindow>:
    name: "food"
    GridLayout:
        cols:1
        Label:
            text:"Pick a Food Type"
        Button:
            text: "Asian"
            on_release:
                app.root.current="asian"
                root.manager.transition.direction="left"
                app.root.current_screen.asianBtn()
        Button:
            text: "Go Back"
            on_release:
                app.root.current="main"
                root.manager.transition.direction="right"

使用AsianWindow类中的asianBtn()方法,gotoLabel的路径更简单,asianBtn()方法本身的路径更简单(因为current_screen是@987654335 @那时)。


更简单的方法是只使用AsianWindowon_enter() 方法,以便在显示AsianWindow 时显示随机选择。为此,只需将 asianBtn() 方法替换为 on_enter() 方法即可:

class AsianWindow(Screen):
    def on_enter(self, *args):
        self.ids.goto.text = random.choice(FoodPlaces['Asian'])

现在您甚至不需要从Button 拨打asianBtn()

<FoodWindow>:
    name: "food"
    GridLayout:
        cols:1
        Label:
            text:"Pick a Food Type"
        Button:
            text: "Asian"
            on_release:
                app.root.current="asian"
                root.manager.transition.direction="left"
        Button:
            text: "Go Back"
            on_release:
                app.root.current="main"
                root.manager.transition.direction="right"

【讨论】:

    【解决方案2】:

    所以您使用的是print,它不会帮助您在 UI 中显示。我不是 100% 确定您想要显示的方式/位置,但我建议您在要显示输出的任何位置添加标签。您可能希望此标签具有 id 属性,以便您可以将其文本值动态更改为随机函数从 dict 中选择的任何值。

    标签在 kv 中看起来像这样:

    Label:
        id: output
    

    要设置它的值,你需要在 py 文件中这样一行:

    self.ids.output.text = "name of restaurant"
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多