Python Forum

Full Version: tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I use tekinter
Using Entry in Tekinter, I want to change the name of my label with exactly what I enter. Now the problem is that I want to do this in a certain number, for example, only 3 times the user can enter the word and change the name of the label. How do I do that?
Create a variable to keep track of the count of entry.
Add an event to the entry that changes the label to the entry's value only if the count is less than 3 and then increase the count.
This should help:

from tkinter import Tk, Button
button_tries = -1

def change_button (event) :
	global button_tries
	tries = ('Second', 'Third')
	button_tries += 1
	if button_tries <= 1 :
		button.configure (text = f'{tries [button_tries]} Try')
 
window = Tk ()
button = Button (window, text = 'First Try')
button.pack (padx = 10, pady = 10)
window.bind ('<Return>', change_button)
window.mainloop ()
Press Return to change the button text.
To change the text of a label you can use a tkinter StringVar:
label_name = StringVar()
Label(root, textvariable=label_name)
label_name.set('Some text')
Or you can use the configure command:
label = Label(root, text'='Initial Label')
label.configure(text='New label text')
Or you can use the fact that many tkinter attributes can be set like the widget is a dictionary.
label = Label(root, text'='Initial Label')
label['text'] = 'New label text'
To have get the text from an entry you will use the get() function. This is most easily done with a StringVar():
entry_text = StringVar()
Entry(root, textvariable=enry_text)
entry_text.set('Some text')
text = entry_text.get()
You will have to write a function that gets the text from the entry and sets the text of the label. Do you know how to do that?

You will have to call the function. You can make a button and when the button is pressed it calls the function. You can also bind events for the Entry. For example, you could call a function when the Enter/Return key is pressed. Which way are you planning to do this?

You will need to keep track of how many times the label is changed. Do you know how to do that?

After the variable is changed three times you should either make the entry not active or erase it. Do you know how to do that.

Sounds like such a simple thing, but there are several decisions to be made and several steps to writing and testing the code.

Good luck.