Reputation: 199
So I'm trying to create a dialog box that asks the user for an input (a number) with python's built-in Tkinter library. In particular, I googled that this could be easily achieved with the method simpledialog.askinteger.
In a normal tkinter.button, I have the argument "command" which allows me to call a method. This is how I first made this part of my code within the main window:
self.generate_game_button = tkinter.Button(self.main_window, text='Start!', \
command=self.create_grid)
But as I want to ask for this number in a pop up window, in tkinter.simpledialog.askinteger, there is no argument for command, so I'm left with no way of calling my create_grid method... The code looks like:
def press_newgame(self):
global a
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
My create_grid method basically makes a set of buttons using the inputted int... How can I achieve this using a pop up window to ask the user for a number, and then call the create grid method similar to how the tkinter.Button works?
I hope this makes sense... Thanks.
Upvotes: 1
Views: 994
Reputation: 1153
I'm not sure a perfectly understand your usecase. If i understand well, you have a "New game" button, and after the user pressed that button, you want to show the askinteger dialog to get the size of the grid you have to generate for the player. In this case, why you just call your your grid-creating function simply after you came back from the dialog, so like:
global a
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
createGrid(size=a) # or whatever your function is
Upvotes: 0
Reputation: 12160
Well, this is working differently than a simple button, because askinteger
is a dialog window, which is not there constantly, it has to be called, and then it will automatically return you a value -- as you expect it.
So I guess you want to do something with the given a
value (you probably want to pass it to the create_grid
method, so all you have to do is call the method after you got the integer value, something like this:
def press_newgame(self):
a = tkinter.simpledialog.askinteger('Inputz', 'Enter the gameboard size')
self.create_grid(a)
Upvotes: 2