import tkinter as tk
# Top level window
frame = tk.Tk()
frame.title("TextBox Input")
frame.geometry('400x200')
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
root = Node("Do you want to go to college?")
root.left = Node("You should consider your options carefully. You do not have to go to college.")
root.right = Node("Are you interested in a liberal arts major?")
root.right.left = Node("Do you need a career very soon?")
root.right.left.left = Node("College makes a lot of sense for you")
root.right.left.right = Node("College will take too much time for you, you should consider trade school")
root.right.right = Node("Are you very wealthy?")
root.right.right.left = Node("Going to college as a liberal arts major is not worth it for you")
def printInput():
temp = root
decision = inputtxt.get()
if decision == "yes":
temp=temp.right
lbl.config(text=temp.data)
if decision == "no":
temp = temp.left
lbl.config(text=temp.data)
inputtxt = tk.Entry(frame, width=20)
inputtxt.pack()
# Button Creation
printButton = tk.Button(frame,
text="Print",
command=printInput)
printButton.pack()
# Label Creation
lbl = tk.Label(frame, text=root.data)
lbl.pack()
frame.mainloop()
When I run the program, it works for the first "yes", but then it just keeps repeating the same output. The issue is in the printInput method, when temp=root gets run, it resets the assignment. How do I make it so that temp=temp.right stays the next time I click the button?
Thank you for any advice you have
CodePudding user response:
If you don't want to reset it then don't use temp=root
inside function but set it outside function.
And inside function use global temp
to inform function that it has to use external variable temp
when you will do temp = ...
- this way it will keep new value when it exits function
import tkinter as tk
# --- classes ---
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# --- functions --- # PEP8: `lower_case_names` for functions
def print_input():
global temp # inform function to use global `temp` when you use `temp = ...`
decision = inputtxt.get()
if decision == "yes":
if temp:
temp = temp.right
if temp:
lbl.config(text=temp.data)
else:
lbl.config(text='No more question')
if decision == "no":
if temp:
temp = temp.left
if temp:
lbl.config(text=temp.data)
else:
lbl.config(text='No more question')
# --- main ---
root = Node("Do you want to go to college?")
root.left = Node("You should consider your options carefully. You do not have to go to college.")
root.right = Node("Are you interested in a liberal arts major?")
root.right.left = Node("Do you need a career very soon?")
root.right.left.left = Node("College makes a lot of sense for you")
root.right.left.right = Node("College will take too much time for you, you should consider trade school")
root.right.right = Node("Are you very wealthy?")
root.right.right.left = Node("Going to college as a liberal arts major is not worth it for you")
temp = root # <--- global variable with start value
# Top level window
frame = tk.Tk()
frame.title("TextBox Input")
frame.geometry('400x200')
inputtxt = tk.Entry(frame, width=20)
inputtxt.pack()
# Button Creation
printButton = tk.Button(frame, text="Print", command=print_input)
printButton.pack()
# Label Creation
lbl = tk.Label(frame, text=root.data)
lbl.pack()
frame.mainloop()