在python中如何使用Tkinter输入变量

2024-04-19 06:48:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何从Tkinter获取输入并将其放入一个变量中,比如电话号码或一段文本。在


Tags: 文本tkinter电话号码
3条回答

您尚未提供任何代码,但可以尝试:

class GetPhoneNbr(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.label = Tk.Label(self.root, text="Enter phone number")
        self.label.pack()

        self.phoneNbr = Tk.StringVar()   # <- here's what you need
        Tk.Entry(self.root, textvariable=self.phoneNbr).pack()

注意:这是一个代码存根,不是整个类定义。在

from tkinter import *


def show_data():
    print( 'My First and Last Name are %s %s' % (fname.get(), lname.get()) )


win = Tk()
Label( win, text='First Name' ).grid( row=0 )
Label( win, text='Last Name' ).grid( row=1 )

fname = Entry( win )
lname = Entry( win )

fname.grid( row=0, column=1 )
lname.grid( row=1, column=1 )

Button( win, text='Exit', command=win.quit ).grid( row=3, column=0, sticky=W, pady=4 )
Button( win, text='Show', command=show_data ).grid( row=3, column=1, sticky=W, pady=4 )

mainloop()

也许这可以帮助你:

from tkinter import *


def get_variable_value():
    valueresult.set( strlname.get() + ' ' + strfname.get() ) #assign val variable to other
    print(valueresult.get()) #if you want see the result in the console


root = Tk()

strfname = StringVar()
strlname = StringVar()
valueresult = StringVar()

labelf = Label(root, text = 'First Name').pack()
fname = Entry(root, justify='left', textvariable = strfname).pack() #strlname get input 

labell = Label(root, text = 'Last Name').pack()
lname = Entry(root, justify='left', textvariable = strlname).pack() #strfname get input 

button = Button(root, text='Show', command=get_variable_value).pack()
res = Entry(root, justify='left', textvariable = valueresult).pack() #only to show result


root.mainloop()

相关问题 更多 >