How can I get the chosen Select Box's Labels from the main function using wxPython?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I get the chosen Select Box's Labels from the main function using wxPython?



I'm sorry for my bad question.I struggled to post my source code because of indent errors.



1.My environment



Python 3.6.3 |Anaconda, Inc.| (default, Oct 6 2017, 12:04:38)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
on darwin



2.Question



How can I get the chosen Select Box's Labels from the main function using wxPython? I made a program using wxPython as follows.I tried it again and again, but I couldn't.Please give your advise.



In the main() function as follows,


print("Main:", sf.getFruitsValue())



shows NULL. I would like to get the chosen Select Box's Labels. In the function OnClick() of the class


NULL


OnClick()


print("OnClick:", self.favorite_fruits)



can show the right chosen labels.



3.Source(Failure)


import os
import wx

class SelectFruits(wx.Frame):

panel=''
layout=''
checkBox=
favorite_fruits=

def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(750, 500))
self.panel = wx.Panel(self, wx.ID_ANY)
self.Centre()
self.Show(True)

def setLayout(self,fruits):
self.layout = wx.BoxSizer(wx.VERTICAL)
self.showMsg1=wx.StaticText(self.panel, wx.ID_ANY, 'Choose the favorite fruits.')
self.layout.Add(self.showMsg1, proportion=1)

for index, target in enumerate(fruits):
(self.checkBox).append(wx.CheckBox(self.panel, wx.ID_ANY, target))
self.layout.Add(self.checkBox[index], flag=wx.GROW)

self.commit_button = wx.Button(self.panel, wx.ID_ANY, "Commit")
self.layout.Add(self.commit_button, proportion=1)
self.SetSizerAndFit(self.layout)
self.Refresh()
self.commit_button.Bind(wx.EVT_BUTTON,self.OnClick)

def OnClick(self,event):
eventObj = event.GetEventObject()
self.favorite_fruits =
for target in self.checkBox:
if target.IsChecked():
self.favorite_fruits.append(target.GetLabel())

print("OnClick:",self.favorite_fruits)

def getFruitsValue(self):
return self.favorite_fruits

def main():
fruits=["Apple","Banana","Grape","Kiwi","Mango","Orange","Pineapple","Strawberry"]
app = wx.App(False)
sf = SelectFruits(None, wx.ID_ANY, "FruitSelect")
sf.setLayout(fruits)
print("Main:",sf.getFruitsValue())
app.MainLoop()
if __name__ == "__main__":
main()





You have posted the code for your question as an answer. Delete this answer and edit the question to include this code. To answer your question, you are already getting the answer which is i.e an empty list. That is because nothing has been selected YET, so self.favorite_fruits is EMPTY.
– Rolf of Saxony
Aug 12 at 10:03





@HiroyasuTakeda Now that you've edited the question, it will be put into a queue for higher reputation users to review and possibly reopen. That queue currently has a couple hundred questions in it, so hopefully your question will be reviewed and reopened in the next 24 hours.
– thesecretmaster
Aug 12 at 14:53





>you are already getting the answer which is i.e an empty list.That is because nothing has been selected YET (My comment) Thank you for your advices. After I selected the checkbox, I could get the names of the selected labels in the function "OnClick()". But I could not get the names of the chosen labels in the main function.
– Hiroyasu Takeda
Aug 18 at 11:29










By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard