【问题标题】:How to use Tkinter input into a variable in python如何在python中使用Tkinter输入到变量中
【发布时间】:2017-10-11 21:06:10
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    您没有提供任何代码,但您可以尝试:

    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()
    

    注意:这是一段代码,而不是整个类定义。

    【讨论】:

    • 很抱歉我没有添加任何代码。我现在的代码在这里:
    • import time from twilio.rest import Client # Your Account SID from twilio.com/console account_sid = "" # Your Auth Token from twilio.com/console auth_token = "" total_breaks = 2 break_count = 0 client = Client(account_sid, auth_token) while(break_count
    • 但我想做的是让用户在窗口中输入的内容成为正文/消息。我问这个的原因是因为我是 Tk 和 python 的新手。
    【解决方案2】:

    也许这可以帮助你:

    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()
    

    【讨论】:

    • 嗨,can't decode 什么意思: res = Entry(root, justify='left', textvariable = valueresult).pack() #只显示结果?这个 Entry 小部件是显示结果还是得到结果?
    • 不会将 res 具有“state=DISABLED”和“value result.set()”选项作为只读条目小部件吗??
    【解决方案3】:
    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()
    

    【讨论】:

      【解决方案4】:

      下面是一个最小的 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-12-24
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2021-11-02
        相关资源
        最近更新 更多