检索 ttk.combobox 的选择值

Retrieve choosen value of a ttk.combobox

我正在创建一个用户界面,其中第一个 window 要求用户在选项列表中选择一个参数。这是一个 MWE :

from tkinter import *
import tkinter.ttk as ttk

Port=''
root = Tk()

PortCOM = ttk.Combobox(root, values=[1,2,3,4,5,6])
PortCOM.bind("<<ComboboxSelected>>",Port)
PortCOM.pack ()

root.mainloop()

print(Port)

所以我已经试过了,还有:

Port = PortCOM.get
Port = PortCOM.cget

最后一次尝试,我收到错误消息:

<bound method Misc.cget of <tkinter.ttk.Combobox object .!combobox>>

例如,如果用户在我的值列表中选择值“4”,我希望它存储在变量 'Port'.

您不需要绑定来跟踪变量。您可以使用 StringVar 来做到这一点。 也就是说,您不能在代码启动时在全局调用 Port = PortCOM.get 并期望得到任何东西。正确语法的第一个问题是 Port = PortCOM.get() 带括号。第二,您在初始化时调用 get(),因此如果不是错误,唯一可能的值将是一个空字符串。

我看到的下一个问题是 bind() 这不是您认为的那样。 bind 用于调用函数而不是直接更新变量。

Combobox 的正确用法是根据值使用 textvariableIntVar()StringVar(),然后在其上使用 get()在函数中需要它时使用 var。

from tkinter import *
import tkinter.ttk as ttk


root = Tk()

textVar = StringVar(root)
textVar.set('')
PortCOM = ttk.Combobox(root, textvariable=textVar, values=[1, 2, 3, 4, 5, 6])
PortCOM.pack()


def print_value():
    print(textVar.get())


Button(root, text='Print Value', command=print_value).pack()

root.mainloop()

如果您出于某种原因真的想使用 bind(),希望在选择后立即执行某些操作,请改用此方法。

确保绑定调用在用于对组合框执行某些操作的函数之后。

from tkinter import *
import tkinter.ttk as ttk


root = Tk()

textVar = StringVar(root)
textVar.set('')
PortCOM = ttk.Combobox(root, textvariable=textVar, values=[1, 2, 3, 4, 5, 6])
PortCOM.pack()

# Note the _ in the argument section of the function.
# A bind will send an event to the function selected unless you use a lambda.
# so to deal with events we don't care about we have a few options.
# We can use an underscore to just except any argument and do nothing with it.
# We could also do event=None but typically I only use that when a function might use the event variable but not always.
def print_value(_): 
    print(textVar.get())
    print(PortCOM.get())


PortCOM.bind("<<ComboboxSelected>>", print_value)

root.mainloop()

如果你不想要额外的按钮,你可以这样做

from tkinter import *
import tkinter.ttk as ttk

Port=''
root = Tk()

def set_port(_):
    global Port
    Port = PortCOM.get()
    print(Port)

PortCOM = ttk.Combobox(root, values=[1,2,3,4,5,6])
PortCOM.bind("<<ComboboxSelected>>", set_port)
PortCOM.pack ()

root.mainloop()

查看此 OO 示例以使用组合的所有功能;)

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.values = ('Apple','Banana','Orange','Grapes','Watermelon','Plum','Strawberries','Pear')

        self.init_ui()

    def init_ui(self):

        self.pack(fill=tk.BOTH, expand=1)

        f = ttk.Frame()

        ttk.Label(f, text = "Combobox").pack()
        self.cbCombo = ttk.Combobox(f,state='readonly',values=self.values)
        self.cbCombo.pack()

        w = ttk.Frame()

        ttk.Button(w, text="Callback", command=self.on_callback).pack()
        ttk.Button(w, text="Reset", command=self.on_reset).pack()
        ttk.Button(w, text="Set", command=self.on_set).pack()
        ttk.Button(w, text="Close", command=self.on_close).pack()

        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)

    def on_callback(self,):

        if self.cbCombo.current() != -1:
            msg = "You have selected:\n{0}".format(self.cbCombo.get())
        else:
            msg = "You did not select anything"

        messagebox.showinfo(self.parent.title(), msg)


    def on_reset(self):
        self.cbCombo.set('')

    def on_set(self):
        self.cbCombo.current(0)

    def on_close(self):
        self.parent.on_exit()

class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)

        self.set_title()
        self.set_style()

        frame = Main(self,)
        frame.pack(fill=tk.BOTH, expand=1)

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")


    def set_title(self):
        s = "{0}".format('Simple App')
        self.title(s)

    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel("Simple App", "Do you want to quit?", parent=self):
            self.destroy()               

if __name__ == '__main__':
    app = App()
    app.mainloop()