Python 2.7 Tkinter question Consider a Python GUI program that produces a window
ID: 3841920 • Letter: P
Question
Python 2.7 Tkinter question
Consider a Python GUI program that produces a window with the following widgets: a text box to display the value of one element of a given list (e.g., a list of the five vowels: a button to retrieve the previous value in that list (if there is one). This button is disabled if there is no previous value in the list a button to retrieve the next value in that list (if there is one). This button is disabled if there is no next value in the list. a label to display the number of the item being displayed and the total number of items (e.g., "1/5") The initial output is as shown below: 76 Vowels prev next 1/5 Hitting the next" button once (from the initial state above) produces the following output: 74 Vowels prev next 2/5Explanation / Answer
Note: As the text in the label is not fixed therefore, we need to create the label in such a way that we attach a string variable to the label. Now, whenever we update the string variable, the text of the label will get updated. The statements to do that -
labeltext = StringVar()
labelCounter = Label(window, textvariable = labeltext)
labelCounter.pack()
Now, the function -
def display_counter():
global counter #It ensures that we want to use the global variable
global labeltext
labeltext.set(str(counter + 1) + "/5") #Setting new string for string variable
Note: As the string gets updated, the label will also display the same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.