【问题标题】:Retrieving and using a tkinter combobox selection检索和使用 tkinter 组合框选择
【发布时间】:2018-05-18 02:19:44
【问题描述】:

我正在为自定义计算器构建一个 GUI,该计算器可以自动将某些测量单位转换为其他测量单位。

我想返回选择的实际文本,以便我可以根据用户选择的任何内容编写 if 语句。如何让 python 返回实际值而不是我现在得到的值?

每当我测试此代码时,我都会收到以下信息:

VirtualEvent 事件 x=0 y=0

以下是我尝试用于此过程的部分代码。对于下面的示例代码,我希望用户能够输入面积为英亩或平方英尺。然后,我计划编写一个 if 语句,将他们选择的任何内容转换为平方公里(此示例中未包含的数字输入代码,以保持本文简洁)。

import tkinter as tk
from tkinter.ttk import *

master = tk.Tk()
master.title("Gas Calculator")
v = tk.IntVar()
combo = Combobox(master)

def callback(eventObject):
    print(eventObject)

comboARU = Combobox(master)
comboARU['values']= ("Acres", "Ft^2")
comboARU.current(0) #set the selected item
comboARU.grid(row=3, column=2)
comboARU.bind("<<ComboboxSelected>>", callback)

master.mainloop()

如果我可以扩展任何内容,请告诉我。我还是 python 的新手,所以如果这只是我缺少的一个简单的语法,我一点也不感到惊讶。

【问题讨论】:

    标签: python python-3.x tkinter combobox


    【解决方案1】:

    您应该使用get() 函数检索comboARU 的内容,如下所示:

    def callback(eventObject):
        print(comboARU.get())
    

    【讨论】:

      【解决方案2】:

      您可以直接从事件对象中检索 Combobox 的值 通过 eventObject.widget.get() 。

      import tkinter as tk
      from tkinter.ttk import *
      
      master = tk.Tk()
      master.title("Gas Calculator")
      v = tk.IntVar()
      combo = Combobox(master)
      
      def callback(eventObject):
          # you can also get the value off the eventObject
          print(eventObject.widget.get())
          # to see other information also available on the eventObject
          print(dir(eventObject))
      
      comboARU = Combobox(master)
      comboARU['values']= ("Acres", "Ft^2")
      comboARU.current(0) #set the selected item
      comboARU.grid(row=3, column=2)
      comboARU.bind("<<ComboboxSelected>>", callback)
      
      master.mainloop()
      

      【讨论】:

        【解决方案3】:

        如果您希望能够使用comboAru.current(0) 设置的默认值,则事件处理不起作用,我发现在按下 OK 按钮时获取组合框值效果最好,如果您想获取该值并在之后使用它,最好创建一个类,避免使用全局变量(因为类实例及其变量在 tkinter 窗口被销毁后仍然存在)(基于答案https://stackoverflow.com/a/49036760/12141765)。

        import tkinter as tk     # Python 3.x
        from tkinter import ttk
        
        class ComboboxSelectionWindow():
            def __init__(self, master):
                self.master=master
                self.entry_contents=None
                self.labelTop = tk.Label(master,text = "Select one of the following")
                self.labelTop.place(x = 20, y = 10, width=140, height=10)
                self.comboBox_example = ttk.Combobox(master,values=["Choice 1","Second choice","Something","Else"])
                self.comboBox_example.current(0)
                self.comboBox_example.place(x = 20, y = 30, width=140, height=25)
        
                self.okButton = tk.Button(master, text='OK',command = self.callback)
                self.okButton.place(x = 20, y = 60, width=140, height=25)
        
            def callback(self):
                """ get the contents of the Entry and exit
                """
                self.comboBox_example_contents=self.comboBox_example.get()
                self.master.destroy()
        
        def ComboboxSelection():
        
            app = tk.Tk()
            app.geometry('180x100')
            Selection=ComboboxSelectionWindow(app)
            app.mainloop()
        
            print("Selected interface: ", Selection.comboBox_example_contents)
        
            return Selection.comboBox_example_contents
        
        print("Tkinter combobox text selected =", ComboboxSelection())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-23
          • 2022-08-18
          • 2018-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-26
          • 1970-01-01
          相关资源
          最近更新 更多