在另一个模块中调用时,tkinter 组合框功能不起作用
tkinter combobox function not working when called in another module
抱歉,这可能是一个新手问题,但我似乎没有找到答案。
我有 2 个 python 模块:source.py 和 work.py. 我想使用 source.py 作为定义一些 tkinter 函数的模块,然后在 work.py.[=12= 中使用它们]
在 source.py 中,我创建了一个组合框函数,它在名为“e”的变量中存储了所需的选项。变量“e”声明为全局变量。
如果我 运行 将 source.py 模块中的组合框函数作为“主要”,变量 e 将在每次 select 组合框中的另一个选项时更改。它按预期工作
但是,如果我在第二个模块“work.py”中导入组合框。那么变量 e 没有改变,而是保持初始赋值。
如何更改脚本,以便在从 work.py 调用组合框函数时更改“e”?
我的模块如下:
source.py:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import ttk
def add_combo_box(root,lst,row,column):
global e
def comboclick(event):
global e
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
options = lst
myCombo = ttk.Combobox(root, value=options, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
#when running here it works ok. Variable "e" is getting changed
if __name__=='__main__':
root = Tk()
e="x"
options = ['one', 'two', 'three', 'four']
add_combo_box(root,options,row=1,column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn=Button(root,text='show value',command=display)
btn.grid(row=2,column=1)
root.mainloop()
work.py:
from tkinter import *
from PIL import ImageTk,Image
from tkinter import ttk
from source import add_combo_box
from tkinter import messagebox
#here it doesn't work. Combobox is launching. Variable "e" is gettting initialised with "y" value but after calling the combobox function it stays the same
if __name__=='__main__':
root = Tk()
global e
e = "y"
options = ['one', 'two', 'three', 'four']
add_combo_box(root, options, row=1, column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
你的问题对我来说仍然是个谜,但我假设你在获取 source.py 内部函数中生成的 Combobox
的值时遇到了问题。您可以只 return 函数中的 Combobox
并为其分配一个变量,然后在其上使用 get()
方法,例如:
def add_combo_box(root,lst,row,column):
def comboclick(event):
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
myCombo = ttk.Combobox(root, value=lst, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
return myCombo #return the combobox
里面 work.py:
if __name__=='__main__':
root = Tk()
e = "y"
options = ['one', 'two', 'three', 'four']
ahem = add_combo_box(root, options, row=1, column=1) #assign a variable to it
def display():
messagebox.showinfo("", str(ahem.get())) #use the get on the variable to get the value of the combobox made
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
为什么这不适用于 e
?在source.py和work.py中创建的e
是不同的,不一样的。事实上,我认为访问 Combobox
属性的正确方法就像我说的那样。
抱歉,这可能是一个新手问题,但我似乎没有找到答案。
我有 2 个 python 模块:source.py 和 work.py. 我想使用 source.py 作为定义一些 tkinter 函数的模块,然后在 work.py.[=12= 中使用它们]
在 source.py 中,我创建了一个组合框函数,它在名为“e”的变量中存储了所需的选项。变量“e”声明为全局变量。
如果我 运行 将 source.py 模块中的组合框函数作为“主要”,变量 e 将在每次 select 组合框中的另一个选项时更改。它按预期工作
但是,如果我在第二个模块“work.py”中导入组合框。那么变量 e 没有改变,而是保持初始赋值。
如何更改脚本,以便在从 work.py 调用组合框函数时更改“e”?
我的模块如下:
source.py:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import ttk
def add_combo_box(root,lst,row,column):
global e
def comboclick(event):
global e
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
options = lst
myCombo = ttk.Combobox(root, value=options, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
#when running here it works ok. Variable "e" is getting changed
if __name__=='__main__':
root = Tk()
e="x"
options = ['one', 'two', 'three', 'four']
add_combo_box(root,options,row=1,column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn=Button(root,text='show value',command=display)
btn.grid(row=2,column=1)
root.mainloop()
work.py:
from tkinter import *
from PIL import ImageTk,Image
from tkinter import ttk
from source import add_combo_box
from tkinter import messagebox
#here it doesn't work. Combobox is launching. Variable "e" is gettting initialised with "y" value but after calling the combobox function it stays the same
if __name__=='__main__':
root = Tk()
global e
e = "y"
options = ['one', 'two', 'three', 'four']
add_combo_box(root, options, row=1, column=1)
def display():
global e
messagebox.showinfo("", str(e))
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
你的问题对我来说仍然是个谜,但我假设你在获取 source.py 内部函数中生成的 Combobox
的值时遇到了问题。您可以只 return 函数中的 Combobox
并为其分配一个变量,然后在其上使用 get()
方法,例如:
def add_combo_box(root,lst,row,column):
def comboclick(event):
e=myCombo.get()
#myLabel = Label(root, text=myCombo.get())
#myLabel.pack()
print('e from combobox: ',e)
# create dropdown box
myCombo = ttk.Combobox(root, value=lst, width=40)
#myCombo.current(0)
myCombo.bind("<<ComboboxSelected>>", comboclick)
myCombo.grid(row=row,column=column)
return myCombo #return the combobox
里面 work.py:
if __name__=='__main__':
root = Tk()
e = "y"
options = ['one', 'two', 'three', 'four']
ahem = add_combo_box(root, options, row=1, column=1) #assign a variable to it
def display():
messagebox.showinfo("", str(ahem.get())) #use the get on the variable to get the value of the combobox made
btn = Button(root, text='show value', command=display)
btn.grid(row=2, column=1)
root.mainloop()
为什么这不适用于 e
?在source.py和work.py中创建的e
是不同的,不一样的。事实上,我认为访问 Combobox
属性的正确方法就像我说的那样。