繁体   English   中英

如何在单独的 function 中从另一个 function 访问变量?

[英]How do I access a variable from another function in a seperate function?

如您所见,我正在尝试从 add_student function 到 see_record function 访问 text_box 变量。 (这不是完整的代码)。 任何帮助,将不胜感激

def add_student(self):
    self.root = tk.Tk()
    self.root.title("ADD STUDENT")
    self.root.geometry("600x400")
    self.root.configure(bg="white")
    self.root.resizable(False, False)

    def save():
        self.root.destroy()
        save_root = tk.Tk()
        save_root.resizable(False, False)

        saved = tk.Label(save_root, text="THE STUDENT HAS BEEN SAVED!", font="Consoles, 16").pack()

        save_root.mainloop()

    header = tk.Label(self.root, text="STUDENT NAME AND AGE GO HERE", font="Consoles, 17").pack()
    # text_box variable
    text_box = tk.Entry(self.root, width=60, font="Consoles, 15")
    text_box.pack(padx=20, pady=20)
    save = tk.Button(self.root, text="SAVE", width=30, height=4, command=save).pack(padx=10, pady=10)

    self.root.mainloop()

def see_record(self):
    record_root = tk.Tk()
    record_root.title("STUDENT RECORDS")
    record_root.geometry("600x400")
    record_root.configure(bg="white")

    scroll = tk.Scrollbar(record_root).pack(side=RIGHT, fill=Y)
    
    # where I am trying to access the text_box variable
    record_label = tk.Label(record_root, text=self.add_student.text_box.get(), font="Consoles, 16")
    record_label.pack()

    record_root.mainloop()

我认为你可以用 self.text_box 解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM