Python3.0Tkinter,如何重复一个函数,每次按下按钮都会得到一个返回值?

Python3.0Tkinter,如何重复一个函数,每次按下按钮都会得到一个返回值?,python,python-3.x,function,tkinter,widget,Python,Python 3.x,Function,Tkinter,Widget,正如您将在下面的代码中看到的,我是一名专业的编码员,但我一直在做这个非常简单的任务 代码的目的是随机生成一个字符串,将该字符串与列表中的一个字符串匹配,然后返回字符串+属性,这样当按钮小部件拉动该字符串时,它将显示整个卡片。(名字和故事)但是,它只生成相同的卡片。我希望它在每次按下按钮时生成一张新卡。是的,我知道指的是回车键,这对我来说只是一种确认它不起作用的简单方法 import _thread import tkinter import tkinter.messagebox import r

正如您将在下面的代码中看到的,我是一名专业的编码员,但我一直在做这个非常简单的任务

代码的目的是随机生成一个字符串,将该字符串与列表中的一个字符串匹配,然后返回字符串+属性,这样当按钮小部件拉动该字符串时,它将显示整个卡片。(名字和故事)但是,它只生成相同的卡片。我希望它在每次按下按钮时生成一张新卡。是的,我知道
指的是回车键,这对我来说只是一种确认它不起作用的简单方法

import _thread
import tkinter
import tkinter.messagebox
import random
import re
import time
import subprocess
from tkinter import *

top = tkinter.Tk()

def presentcard():
    tkinter.messagebox.showinfo("this is your card", what)    

thebutton = tkinter.Button(top, text = "Pull a card out.", command = presentcard)

class Allthecards:
    def __init__(self,name,story):
        self.name = name
        self.story = story

card1 = Allthecards("The Beast", "It eats things")
card2 = Allthecards("The Dog", "It barks at things")
card3 = Allthecards("The Jazz", "It hears things")
card4 = Allthecards("The Candles", "It feels things")
card5 = Allthecards("The Heat", "It burns things")

cardlist = ['card1','card2','card3','card4','card5']

def getcard():
    return(random.choice(cardlist))
thecard =  getcard()

def whichcard():
    if thecard == "card1":
        return(card1.name + " "+ card1.story)
    elif thecard == "card2":
        return(card2.name + " "+ card2.story)
    elif thecard == "card3":
        return(card3.name + " "+ card3.story)
    elif thecard == "card4":
        return(card4.name + " "+ card4.story)
    elif thecard == "card5":
        return(card5.name + " "+ card5.story)
    else:
        return thecard

what = whichcard()

thebutton.bind('<Return>',thecard)
thebutton.bind('<Return>',what)
thebutton.pack()    
top.mainloop()
import\u线程
进口tkinter
导入tkinter.messagebox
随机输入
进口稀土
导入时间
导入子流程
从tkinter进口*
top=tkinter.Tk()
def presentcard():
tkinter.messagebox.showinfo(“这是你的卡”,什么)
按钮=tkinter.Button(顶部,text=“拔出卡片”,命令=presentcard)
分类所有卡片:
定义初始(自我、姓名、故事):
self.name=名称
self.story=故事
卡片1=所有卡片(“野兽”,“它吃东西”)
卡片2=所有卡片(“狗”,“它对着东西吠叫”)
card3=所有卡片(“爵士”,“它能听到东西”)
卡片4=所有卡片(“蜡烛”,“它能感觉到东西”)
卡片5=所有卡片(“热量”,“它燃烧东西”)
卡片列表=['card1'、'card2'、'card3'、'card4'、'card5']
def getcard():
返回(随机选择(卡片列表))
thecard=getcard()
def whichcard():
如果卡片==“卡片1”:
返回(card1.name+“”+card1.story)
elif thecard==“card2”:
返回(card2.name+“”+card2.story)
elif thecard==“card3”:
返回(card3.name+“”+card3.story)
elif thecard==“card4”:
返回(card4.name+“”+card4.story)
elif thecard==“card5”:
返回(card5.name+“”+card5.story)
其他:
退票
what=哪张卡()
按钮。绑定(“”,卡片)
按钮。绑定(“”,什么)
thebutton.pack()
top.mainloop()

您只调用了一次
getcard()
方法。这就是为什么你每次都看到相同的信息

将您的
presentcard()
更改为以下内容

def presentcard():
    global thecard
    thecard = getcard()
    what = whichcard()
    tkinter.messagebox.showinfo("this is your card", what)
它会给你想要的

此外,如果将参数传递给
def whichcard()
函数,如
def whichcard(thecard)
,则不必在程序中的任何地方使用
global thecard
。因此,函数将更改为

def presentcard():
    thecard = getcard()
    what = whichcard(thecard)
    tkinter.messagebox.showinfo("this is your card", what)

避免像在代码中使用的那样使用全局变量。重用这些函数变得很困难。只需将参数传递给函数。

请查看是否有更好的代码段。您的第二个
bind
将覆盖第一个。另外,请注意上面的
bind
s实际上没有任何作用。我不认为你传递了正确的论点。嗨,谢谢你的时间,我会看看你的链接,看看我能学到什么。嗨,谢谢你解决了我的问题。我经常阅读指南和教程,他们总是说同样的话“不要使用全局变量”。我没有想到仅仅把something=function放在任何地方就可以使它成为全局变量,尽管这很有意义,因为我没有把它与特定的类或任何东西联系起来。我没有想到要将这些功能与按钮功能联系起来。