getting selected value of Combobox python

后端 未结 2 762
借酒劲吻你
借酒劲吻你 2021-01-28 22:01

Hi I have the following code

def create(self):
    geo = StringVar()
    city = ttk.Combobox(gui, textvariable=geo,state=\"readonly\")
    city.config(values=se         


        
相关标签:
2条回答
  • 2021-01-28 22:25

    this worked for me :

    def create(self):
            print(self.geo)
            strgeo="\n".join(str(x) for x in self.geo)
    
            print(strgeo)
            city = ttk.Combobox(gui, textvariable=self.stringGeo, state="readonly",width=30)
            city.config(values=strgeo)
            city.pack()
            city.bind("<<ComboboxSelected>>",self.selectedCity)
    
        def selectedCity(self,event):
            selected=self.stringGeo.get()
    
    0 讨论(0)
  • 2021-01-28 22:29

    As it stands you cannot retrieve the value of geo, since it is not defined as an attribute of your class, but in the local scope of create. What you can do is declaring geo as a static attribute and then call it from within your method when you need it.

    class(object):
    
        geo = StringVar()
        geos = ('NY','LA','RY','...')
    
        def __init__(self,#....
            #...
    
        def create(self):
            city = ttk.Combobox(gui, textvariable=self.geo,state="readonly")
            city.config(values=self.geos) 
            city.pack()
            city.bind("<<ComboboxSelected>>", self.cityselection)
    
    
        def cityselection(self,event):
            selected=self.geo.get()
            print(selected)
    

    Actually, event is not what you think it is.

    0 讨论(0)
提交回复
热议问题