问题描述
我正在尝试使用tkinter制作剪刀石头布,并且我有一个用户在其中写石头,纸张或剪刀的条目
players_choice_entry = tkinter.Entry()
def check():
players_choice = players_choice_entry.get()
check = tkinter.Button( text="Play",command=check)
我如何才能全局设置players_choice并在功能之外使用它?
解决方法
在运行函数之前,需要设置一个变量,例如players-choice
。在函数中,将变量声明为全局变量,然后运行所编写的代码行。在一起看起来像这样:
players_choice = ''
players_choice_entry = tkinter.Entry()
def check():
global players_choice
players_choice = players_choice_entry.get()
check = tkinter.Button( text="Play",command=check)