[英]python Tkinter get() value from Entry Field
我從Tkinter()輸入字段中獲取值時感到困惑。 我有這種代碼......
from Tkinter import*
def valueGET(val1, val2):
print val1 + " " + val2
class ContentUI():
def showLogin(self, frame):
self.contentUI = ContentUI()
L1 = Label(frame, text="Name")
L1.pack( side = LEFT)
L1.grid()
E1 = Entry(frame, bd =5)
E1.pack(side = RIGHT)
E1.grid()
L2 = Label(frame, text="Secret")
L2.pack( side = LEFT)
L2.grid()
E2 = Entry(frame, bd =5, show="*")
E2.pack(side = RIGHT)
E2.grid()
submit = Button(frame, text="Enter", width=15, command=valueGET(E1.get(), E2.get()))
submit.grid()
class UIDisplay():
def play(self):
root = Tk()
root.title(title)
root.geometry(dimension)
app = Frame(root)
contentUI = ContentUI()
contentUI.showLogin(app)
app.grid()
root.mainloop()
adkooPlay = UIDisplay()
adkooPlay.play()
但似乎我的valueGET(val1,val2)方法不起作用,它沒有打印值,甚至沒有通過使用Tkinter()Entry的get()方法得到它,我做錯了嗎? 應該怎么做?
提前致謝 ...
甚至在創建submit
按鈕之前,代碼調用valueGET
。 然后它將函數的返回值作為command
參數傳遞給Button構造函數。
要將該函數注冊為回調,請替換folloiwng行:
submit = Button(frame, text="Enter", width=15, command=valueGET(E1.get(), E2.get()))
有:
submit = Button(frame, text="Enter", width=15, command=lambda: valueGET(E1.get(), E2.get()))
您需要在函數的print命令中使用括號
def valueGET(val1, val2):
print val1 + " " + val2
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.