python-3.x 我不明白为什么我不能得到一个组合框值

n53p2ov0  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(104)

所有人
这个程序很简单,我有一个组合框,有两个选项,通过选择其中一个选项,一个新的窗口出现,接下来它应该有一个更新的列表,这是依赖于以前的选择。然后如果用户选择了任何更新的值,我有另一个函数接受该值,并应将其反映在初始窗口上。
我不能通过if条件,python没有给我任何错误或警告;但是,如果我在新窗口中选择了一个不可见的选项,python会抛出一个错误,说combo_2没有定义。
我不明白我做错什么了?
代码如下:

from tkinter import *
from tkinter import ttk 

root = Tk()
root.title('Example')

root.geometry('415x415')

starting_list = ['Option A', 'Option B']

def updating_lists(event):
    list_A = ['A_1', 'A_2', 'A_3']
    list_B = ['B_1', 'B_2', 'B_3']
    final_list = ['']
    top_1 = Toplevel()
    top_1.title('This window should have an updated list')
    top_1.geometry('220x220')
    
    if combo_1.get() == 'Option A':
        print('you have selected option A')
        final_list = list_A
        
    elif combo_1.get() == 'Option B':
        print('you have selected option B')
        final_list = list_B
        
    combo_2 = ttk.Combobox(top_1, values = final_list, state = 'readonly')
    combo_2.pack()
    combo_2.bind('<<ComboboxSelected>>', update_values)
    
def update_values(event):    
    variable_1 = combo_2.get()
    print(f'variable combo_2.get() is: {combo_2.get()}')
    print(f'variable_1 is: {variable_1}')
    top_1.destroy()
    return(variable_1)
    
###########################################
#               Frame A                   #
###########################################

frame_A = LabelFrame(root, width = 400, height = 400, text = 'Example', padx = 5, pady = 5)
frame_A.grid_propagate(0)
frame_A.grid(row = 0, column = 0, padx = 10, pady = 5)

variable_1 = StringVar(root, '')

combo_1 = ttk.Combobox(frame_A, values = starting_list, state = 'readonly', width = 15)
combo_1.set('Choose one')
combo_1.grid(row = 0, column = 0)
combo_1.bind('<<ComboboxSelected>>', updating_lists)

label_1 = Label(frame_A, textvariable = variable_1, padx = 5, pady = 5)
label_1.grid(row = 1, column = 0)

root.mainloop()

我用的是windows 10,用的是python 3。

cgvd09ve

cgvd09ve1#

正如Bryan奥克利所指出的,我的代码中有一些错误,第一个是if条件(真的很愚蠢,所以忘记它),第二个错误是我假设combo_2是全局的,它不是,所以我所做的是将top_1和combo_2声明为全局变量。我试图将另一个变量声明为全局变量,但这不起作用,因为我的第三个错误是假设.bind正在停止流,但它没有,它什么也没有填充这个变量。
正在添加正确的代码...我认为这不是最好的,但它的工作。

from tkinter import *
from tkinter import ttk

root = Tk()
root.title('Example')

root.geometry('415x415')

starting_list = ['Option A', 'Option B']

def updating_lists(event):
    list_A = ['A_1', 'A_2', 'A_3']
    list_B = ['B_1', 'B_2', 'B_3']
    final_list = ['']
    global top_1
    global combo_2
    top_1 = Toplevel()
    top_1.title('This window should have an updated list')
    top_1.geometry('220x220')
    
    if combo_1.get() == 'Option A':
        print('you selected option A')
        final_list = list_A
        
    elif combo_1.get() == 'Option B':
        print('you selected option B')
        final_list = list_B
        
    combo_2 = ttk.Combobox(top_1, values = final_list, state = 'readonly')
    combo_2.pack()
    combo_2.bind('<<ComboboxSelected>>', terminate_top)
    
def terminate_top(event):
    print(f'combo_2.get is: {combo_2.get()}')
    label_1 = Label(frame_A, text = str(combo_2.get()), padx = 5, pady = 5)
    label_1.grid(row = 1, column = 0)
    top_1.destroy()
    
###########################################
#               Frame A                   #
###########################################

frame_A = LabelFrame(root, width = 400, height = 400, text = 'Example', padx = 5, pady = 5)
frame_A.grid_propagate(0)
frame_A.grid(row = 0, column = 0, padx = 10, pady = 5)

combo_1 = ttk.Combobox(frame_A, values = starting_list, state = 'readonly', width = 15)
combo_1.set('Choose one')
combo_1.grid(row = 0, column = 0)
combo_1.bind('<<ComboboxSelected>>', updating_lists)

root.mainloop()

非常感谢你的提示!

相关问题