Using the variable from entry/button in another function in Tkinter

后端 未结 1 404
心在旅途
心在旅途 2021-01-25 13:15

When I press the button, I want it to get the Entry and -for future things- use it in another function.

import tkinter

def ButtonAction():
    MyEn         


        
1条回答
  •  醉梦人生
    2021-01-25 13:53

    Structure the program using class.

    import tkinter
    
    class Prompt:
        def button_action(self):
            self.my_entry = self.ent.get() #this is the variable I wanna use in another function
    
        def __init__(self, den):
            self.lbl = tkinter.Label(den, text="Write Something")
            self.ent = tkinter.Entry(den)
            self.btn = tkinter.Button(den, text="Get That Something", command=self.button_action)
            self.lbl.pack()
            self.ent.pack()
            self.btn.pack()
    
    den = tkinter.Tk()
    den.title("Widget Example")
    prompt = Prompt(den)
    den.mainloop()
    

    You can access the input using prompt.my_entry later.

    0 讨论(0)
提交回复
热议问题