如何在python中使用Tkinter输入变量
我想知道如何从Tkinter获取输入并将其放入变量(如电话号码或文本)中。您尚未提供任何代码,但可以尝试:如何在python中使用Tkinter输入变量,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我想知道如何从Tkinter获取输入并将其放入变量(如电话号码或文本)中。您尚未提供任何代码,但可以尝试: 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
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()
类GetPhoneNbr(对象):
定义初始化(自):
self.root=Tk.Tk()
self.label=Tk.label(self.root,text=“输入电话号码”)
self.label.pack()
self.phoneNbr=Tk.StringVar()#也许这可以帮助您:
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()
下面是一个最小的GUI,它将在条目中写入的内容放入变量,即按钮的文本:
import tkinter as tk
def replace_btn_text():
b['text'] = e.get()
root = tk.Tk()
e = tk.Entry(root)
b = tk.Button(root, text="Replace me!", command=replace_btn_text)
e.pack()
b.pack()
root.mainloop()
同一示例,但具有OOP结构:
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self._create_widgets()
self._display_widgets()
def replace_btn_text(self):
""" Replaces button's text with what's written in the entry.
"""
# self._e.get() returns what's written in the
self._b['text'] = self._e.get() # entry as a string, can be assigned to a var.
def _create_widgets(self):
self._e = tk.Entry(self)
self._b = tk.Button(self, text="Replace me!", command=self.replace_btn_text)
def _display_widgets(self):
self._e.pack()
self._b.pack()
if __name__ == '__main__':
root = App()
root.mainloop()
很抱歉,我没有添加任何代码。我现在拥有的代码如下:从twilio.rest导入客户端导入时间#从twilio.com/console Account\u SID=“”#从twilio.com/console Auth\u Token=“”)从twilio.com/console Auth\u Token=“total\u breaks=2 break\u count=0 Client=Client(Account\u SID,Auth\u Token)而(break\u countimport tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self._create_widgets()
self._display_widgets()
def replace_btn_text(self):
""" Replaces button's text with what's written in the entry.
"""
# self._e.get() returns what's written in the
self._b['text'] = self._e.get() # entry as a string, can be assigned to a var.
def _create_widgets(self):
self._e = tk.Entry(self)
self._b = tk.Button(self, text="Replace me!", command=self.replace_btn_text)
def _display_widgets(self):
self._e.pack()
self._b.pack()
if __name__ == '__main__':
root = App()
root.mainloop()