二叉树 Tkinter

Binary Tree Tkinter

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()

当我 运行 程序时,它对第一个“是”起作用,但随后它只是不断重复相同的输出。问题出在 printInput 方法中,当 temp=root 获取 运行 时,它会重置分配。我如何才能使 temp=temp.right 在我下次单击按钮时保持不变?

感谢您的任何建议

如果您不想重置它,那么不要在函数内部使用 temp=root,而是在函数外部设置它。

并且在函数内部使用 global temp 通知函数它必须使用外部变量 temp 当你要执行 temp = ... - 这样它会在退出函数时保留新值

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()

PEP 8 -- Style Guide for Python Code