Python 我怎么能叫“a”;“命令”;用tkinter.simpledialog?
因此,我试图创建一个对话框,要求用户使用python内置的Tkinter库输入(一个数字)。特别是,我在谷歌上搜索到,使用simpledialog.askinteger方法很容易实现这一点 在一个普通的tkinter.button中,我有一个参数“command”,它允许我调用一个方法。这是我第一次在主窗口中制作这部分代码的方式:Python 我怎么能叫“a”;“命令”;用tkinter.simpledialog?,python,user-interface,tkinter,Python,User Interface,Tkinter,因此,我试图创建一个对话框,要求用户使用python内置的Tkinter库输入(一个数字)。特别是,我在谷歌上搜索到,使用simpledialog.askinteger方法很容易实现这一点 在一个普通的tkinter.button中,我有一个参数“command”,它允许我调用一个方法。这是我第一次在主窗口中制作这部分代码的方式: self.generate_game_button = tkinter.Button(self.main_window, text='Start!', \
self.generate_game_button = tkinter.Button(self.main_window, text='Start!', \
command=self.create_grid)
但是,由于我想在tkinter.simpledialog.askinteger的弹出窗口中询问这个号码,所以命令没有参数,所以我无法调用我的create_grid方法。。。代码如下所示:
def press_newgame(self):
global a
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
我的create_grid方法基本上是使用输入的int。。。如何使用一个弹出窗口询问用户一个号码,然后调用与tkinter.Button类似的创建网格方法来实现这一点
我希望这是有意义的。。。谢谢。好吧,这与一个简单的按钮不同,因为askinteger是一个对话框窗口,它不经常出现,必须调用它,然后它会自动返回一个值——正如您所期望的那样 因此,我猜您希望使用给定的
a
值执行某些操作(您可能希望将其传递给create\u grid
方法,因此您所要做的就是在获得整数值后调用该方法,如下所示:
def press_newgame(self):
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
self.create_grid(a)
我不确定你是否完全理解你的用例。如果我理解得很好,你就有了一个“新游戏”按钮,用户按下该按钮后,您希望显示askinteger对话框,以获取必须为播放器生成的网格的大小。在这种情况下,为什么您只需在从对话框返回后调用网格创建函数,如下所示:
global a
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
createGrid(size=a) # or whatever your function is