设置ttk Combobox的默认值

时间:2021-08-16 17:08:57

I'm using Python 3.2.1 in Arch Linux x86_64. This one is really driving me crazy: I just want to have a default, preselected value for a ttk.Combobox as soon as I grid it. This is my code:

我在Arch Linux x86_64中使用Python 3.2.1。这个真的让我抓狂:我只想在我网格化时为ttk.Combobox设置一个默认的预选值。这是我的代码:

from tkinter import Tk, StringVar, ttk

root = Tk()

def combo(parent):
    value = StringVar()
    box = ttk.Combobox(parent, textvariable=value, state='readonly')
    box['values'] = ('A', 'B', 'C')
    box.current(0)
    box.grid(column=0, row=0)

combo(root)

root.mainloop()

Which draws an empty Combobox. What's funny is that if I don't use a function it works perfectly:

其中绘制了一个空的Combobox。有趣的是,如果我不使用某个功能,它可以很好地工作:

from tkinter import Tk, StringVar, ttk

root = Tk()

value = StringVar()
box = ttk.Combobox(root, textvariable=value, state='readonly')
box['values'] = ('A', 'B', 'C')
box.current(0)
box.grid(column=0, row=0)

root.mainloop()

Of course, in the real program I have to use a function, so I need another solution.

当然,在真正的程序中我必须使用一个函数,所以我需要另一个解决方案。

3 个解决方案

#1


23  

The problem is that the instance of StringVar is getting garbage-collected. This is because it's a local variable due to how you wrote your code.

问题是StringVar的实例正在被垃圾收集。这是因为由于您编写代码的方式,它是一个局部变量。

One solution is to use a class so that your StringVar persists:

一种解决方案是使用类,以便StringVar持久化:

from tkinter import Tk, StringVar, ttk

class Application:

    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value, 
                                state='readonly')
        self.box['values'] = ('A', 'B', 'C')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()

#2


4  

When your function 'combo' exits, the local variable 'value' is destroyed. You need a persistent variable, such as a global variable or a variable that is a property of a class so that the value isn't garbage-collected while the widget still exists.

当你的函数'combo'退出时,会破坏局部变量'value'。您需要一个持久变量,例如全局变量或作为类属性的变量,以便在窗口小部件仍然存在时不会对该值进行垃圾收集。

#3


0  

The get() method can be used within your function to rename the StringVar and save it under another name to avoid losing it altogether via garbage collection.

可以在函数中使用get()方法重命名StringVar并将其保存在另一个名称下,以避免通过垃圾回收完全丢失它。

value = StringVar()

keepvalue = value.get()

then use keepvalue instead of value:

然后使用keepvalue而不是value:

box = ttk.Combobox(root, textvariable=keepvalue, state='readonly')

This had 'A' showing in the combobox for me.

这对我来说在组合框中显示'A'。

#1


23  

The problem is that the instance of StringVar is getting garbage-collected. This is because it's a local variable due to how you wrote your code.

问题是StringVar的实例正在被垃圾收集。这是因为由于您编写代码的方式,它是一个局部变量。

One solution is to use a class so that your StringVar persists:

一种解决方案是使用类,以便StringVar持久化:

from tkinter import Tk, StringVar, ttk

class Application:

    def __init__(self, parent):
        self.parent = parent
        self.combo()

    def combo(self):
        self.box_value = StringVar()
        self.box = ttk.Combobox(self.parent, textvariable=self.box_value, 
                                state='readonly')
        self.box['values'] = ('A', 'B', 'C')
        self.box.current(0)
        self.box.grid(column=0, row=0)

if __name__ == '__main__':
    root = Tk()
    app = Application(root)
    root.mainloop()

#2


4  

When your function 'combo' exits, the local variable 'value' is destroyed. You need a persistent variable, such as a global variable or a variable that is a property of a class so that the value isn't garbage-collected while the widget still exists.

当你的函数'combo'退出时,会破坏局部变量'value'。您需要一个持久变量,例如全局变量或作为类属性的变量,以便在窗口小部件仍然存在时不会对该值进行垃圾收集。

#3


0  

The get() method can be used within your function to rename the StringVar and save it under another name to avoid losing it altogether via garbage collection.

可以在函数中使用get()方法重命名StringVar并将其保存在另一个名称下,以避免通过垃圾回收完全丢失它。

value = StringVar()

keepvalue = value.get()

then use keepvalue instead of value:

然后使用keepvalue而不是value:

box = ttk.Combobox(root, textvariable=keepvalue, state='readonly')

This had 'A' showing in the combobox for me.

这对我来说在组合框中显示'A'。