python Tkinter get() value from Entry Field

前端 未结 3 1176
感情败类
感情败类 2020-12-10 09:31

I have getting confused in getting the value from the Tkinter() Entry Field. I have this kind of code...

from Tkinter import*

def valueGET(val1, val2):
             


        
相关标签:
3条回答
  • 2020-12-10 10:18
    from tkinter import *
    import tkinter as tk
    root =tk.Tk()
    mystring =tk.StringVar(root)
    def getvalue():
        print(mystring.get())
    e1 = Entry(root,textvariable = mystring,width=100,fg="blue",bd=3,selectbackground='violet').pack()
    button1 = tk.Button(root, 
                    text='Submit', 
                    fg='White', 
                    bg= 'dark green',height = 1, width = 10,command=getvalue).pack()
    
    root.mainloop()
    
    0 讨论(0)
  • 2020-12-10 10:30

    you need brackets in the print command in your function

    def valueGET(val1, val2):
        print val1 + "  " + val2
    
    0 讨论(0)
  • 2020-12-10 10:31

    The code call valueGET even before submit button is created. Then it pass the return value of the function to Button constructor as command argument.

    To register the function as callback, replace folloiwng line:

    submit = Button(frame, text="Enter", width=15, command=valueGET(E1.get(), E2.get())) 
    

    with:

    submit = Button(frame, text="Enter", width=15, command=lambda: valueGET(E1.get(), E2.get())) 
    
    0 讨论(0)
提交回复
热议问题