简体   繁体   English

Python 3.x tkinter组合框同步['variables']

[英]Python 3.x tkinter comboboxes syncing ['variables']

I am trying to have multiple (two for this example) ttk.Comboboxes with unique variables. 我正在尝试使用唯一变量的多个(此示例为两个)ttk.Comboboxes。 The boxes are syncing my selection rather than allowing me to select individual variables. 这些框将同步我的选择,而不是允许我选择单个变量。

I am using a main file and an import to store the variable: 我正在使用一个主文件和一个导入来存储变量:

list.py (the variables) list.py (变量)

class object():
    def __init__(self, name):
        self.name = name
        self.list = ['a','b','c']

main.py (the program): main.py (程序):

from tkinter import *
from tkinter import ttk

import list

root = Tk()

aList = list.object('aName')
bList = list.object('bName')

aVariable = aList.list
aCombobox = ttk.Combobox(root, textvariable=aVariable)
aCombobox['values'] = aList.list
aCombobox.grid()

bVariable = bList.list
bCombobox = ttk.Combobox(root, textvariable=bList.list)
bCombobox['values'] = bList.list
bCombobox.grid()

root.mainloop()

importing the variables (as a module/class) and initializing each (aList and bList) I figure would create two separate objects. 导入变量(作为模块/类)并初始化每个变量(aList和bList),我将创建两个单独的对象。 Each Combobox has its own textvariable and generates its ['valuse'] from the created objects. 每个组合都有其自己的text变量,并从创建的对象生成其['valuse']。 However, this does not work - they keep syncing. 但是,这不起作用-他们一直保持同步。

I was able to get it to work in an interactive session as well as a CLI application: 我能够使其在交互式会话以及CLI应用程序中工作:

list.py (same import as above) list.py (与上面的导入相同)

main.py (as a console - no tkinter) main.py (作为控制台-无tkinter

import list

a = list.object('list')
b = list.object('list')

print(a.list, b.list)

b.list.pop()

print(a.list, b.list)

input()

I've also tried copy.copy() and copy.deepcopy() but neither work! 我也尝试过copy.copy()和copy.deepcopy(),但是都没有用!

You cannot use ordinary variables as the value of the textvariable attribute. 您不能将普通变量用作textvariable属性的值。 You'll need to use a tkinter variable -- typically an instance of StringVar . 您将需要使用tkinter变量-通常是StringVar的实例。 For more information see http://effbot.org/tkinterbook/variable.htm 有关更多信息,请参见http://effbot.org/tkinterbook/variable.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM