-1

我希望第二个Comboxcbo_forecast_ps在选择第一个组合中的项目时显示某些值cbo_forecast_w

from Tkinter import *
import tkMessageBox
import ttk
from ttk import *

masterframe =Tk()
# Create right frame for general information
rightFrame = Frame(masterframe,width 
=600,height=300,borderwidth=2,relief=SOLID,)
rightFrame.place(x=610,y=0)

for_w_text =StringVar()
cbo_forecast_w = ttk.Combobox (rightFrame, textvariable=for_w_text)
cbo_forecast_w['values']=("cow","chicken","ant")

string_text =StringVar()
cbo_forecast_ps = ttk.Combobox (rightFrame,textvariable=string_text)

def choosestring():
    forecast_w= for_well_text.get()
    lbl_test.configure(text = forecast_w)
    if forecast_w=="cow":
        cbo_forecast_ps['values'] = ("single")
        cbo_forecast_ps.current(0)
    else:
        cbo_forecast_ps['values'] = ("Short", "Long")
        cbo_forecast_ps.current(0)        

# I hope this is correct

cbo_forecast_w.bind("<<ComboboxSelected>>",choosestring())

我发现forecast_w没有从for_well_text.get(). 相反,它正在放弃PY_VAR2

如何解决这个问题?

4

1 回答 1

0

这是您发布的代码的简化版本,您的代码中有一些错误是您没有创建label来配置它的,并且在您的评论中您说您正在获取PY_VAR2您应该get用来接收combobox.

要接收小部件中的内容,您可以使用for_w_text.get()而不使用 ,stringvar就像您在cbo_forecast_pswidget.

from Tkinter import *
import tkMessageBox
import ttk
from ttk import *


def choosestring(event=None):
    nf = cbo_forecast_w.get() # get the conten in the combo box

    lbl_test.configure(text = nf)  # label to configure
    if for_w_text.get()=="cow":
        cbo_forecast_ps['values'] = ("single")
        cbo_forecast_ps.current(0)
    else:
        cbo_forecast_ps['values'] = ("Short", "Long")
        cbo_forecast_ps.current(0)

# I hope this is correct


masterframe =Tk()
# Create right frame for general information
rightFrame = Frame(masterframe,width
=600,height=300,borderwidth=2,relief=SOLID,)
rightFrame.place(x=610,y=0)

for_w_text =StringVar()
cbo_forecast_w = ttk.Combobox (rightFrame, textvariable=for_w_text)
cbo_forecast_w['values']=("cow","chicken","ant")
cbo_forecast_w.pack()


string_text =StringVar()
cbo_forecast_ps = ttk.Combobox (rightFrame,textvariable=string_text)
cbo_forecast_ps.pack()


lbl_test = Label(rightFrame)
lbl_test.pack()


cbo_forecast_w.bind("<<ComboboxSelected>>",choosestring)

masterframe.mainloop()
于 2018-09-13T11:13:53.077 回答