![]() Whenever you press a key in the range (0-9), it will display a message on the screen. Running the above code snippet will display a window with a Label widget. # Bind all the number keys with the callback function Label=Label(win, text="Press any key in the range 0-9") Label(win, text="You have pressed: " + e.char, font='Arial 16 bold').pack() # Function to display a message whenever a key is pressed Using the bind("", callback) function, we can also bind all the number keys to display a message on the screen such that whenever a user presses a key (1-9), a message will appear on the screen. Whenever we bind a key with an event, the callback event occurs whenever a corresponding key is pressed. You can use bind(, callback) function for each widget that you want to bind in order to perform a certain type of event. Tkinter provides a mechanism to deal with such events. ![]() When you want to change the value of the variable, you'd need to call the set() method of the variable object (see onClick) instead of assigning the value directly to it.While developing a Tkinter application, we often encounter cases where we have to perform some specific operation or event with the keystrokes (on keyboard). Instead of passing the value this variable holds to the text attribute of the Label, we assign the variable to textvariable attribute, so when the value of the variable gets updated, Label would update the displayed text accordingly. Tkinter.Button(root, text="Increase", command=onClick, fg="dark green", bg = "white").pack() Tkinter.Label(root, textvariable=counter).pack() Here is a look alike code to the one in the question, but instead of defining counter as a normal variable, it is a variable from Tkinter. ![]() They are specially useful when you need to modify a data that other widgets might interact with. MButton1 = Button(text = counter, command = nClick, fg = "darkgreen", bg = "white") Here is the final solution (changes button value and has no label, but this is easily changed): from tkinter import * config(text = counter) which configures the button. Now to update the text in the button, you use. You can get rid of the second function as you only need one, then you change the command of the button and its text to counter. Now the code will be able to count, but you have set variables as Buttons, but then made them None type objects, to change this. Also you must change counter + 1 to counter += 1 so it increments the variable (Tk itself is not part of Python it is maintained at ActiveState. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. Press the close button from our application to exit the game completely. Press the main close button to restart the game with new number. If the label shows correct then show the number of attempts. ![]() Next you must deifne the counter variable as a global variable, so that it will be the same in the function (do this inside the function as well). The tkinter package (Tk interface) is the standard Python interface to the Tk GUI toolkit. Guess a new number if the label doesn’t show correct. My answer basically changes what you have already into the easiest way for it to do what you want.įirstly you import libraries that you don't need/use (you may need them in your whole code, but for this question include a minimal example only). Ok so there are a few things wrong with your code so far.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |