0

Hi
I'm having some trouble with my code. In my code, I've designed a frame with a checked box inside using wx.Frame and wx.CheckBox. I'm trying to allow users to decide whether they want to grayscale a list of images. If the user checks the box, wx.ProgressDialog is launched. All seems well, except the after theProgressDialog finishes, the frame does not close. I can't figure it out!!
Excerpt of the code below

Please advise


def grayscale_progress(path,imagelist):
app=wx.PySimpleApp()
dialog=wx.ProgressDialog("Grayscaling", "Progress", len(imagelist), style=
wx.PD_ESTIMATED_TIME
| wx.PD_REMAINING_TIME
| wx.PD_AUTO_HIDE)
count = 0
for num,images in enumerate(imagelist):
gray = Image.open(imagelist[num]).convert("L")
gray.save(os.path.join(path,images))
count += 1
dialog.Update(count)
wx.MilliSleep(500)
dialog.Destroy()


def check(path,imagelist):
app = wx.PySimpleApp()
frame=wx.Frame(None,-1,"Test",size=(250,150))
panel=wx.Panel(frame,-1)
label=wx.StaticText(panel,-1,"What to do next?",(35,20))
button=wx.Button(panel,-1,"Apply",(90,90))
gray=wx.CheckBox(panel,-1,"Gray",(35,40),(150,30))

def onclick(event):
if gray.GetValue():
grayscale_progress(path,imagelist)
frame.Destroy()

button.Bind(wx.EVT_BUTTON,onclick)

frame.Show()
app.MainLoop()

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Paul Thompson
0

There are a couple of issues i can see. The first of which is a lack of code tags.

[code=python]

[/code]

This is the corrected code.. i think, some indentation might be out depending on when i thought a certain loop might end.

def grayscale_progress(path,imagelist):
    app=wx.PySimpleApp()
    dialog=wx.ProgressDialog("Grayscaling", "Progress", len(imagelist), style=
    wx.PD_ESTIMATED_TIME
    | wx.PD_REMAINING_TIME
    | wx.PD_AUTO_HIDE)
    count = 0
    for num,images in enumerate(imagelist):
        gray = Image.open(imagelist[num]).convert("L")
        gray.save(os.path.join(path,images))
        count += 1
        dialog.Update(count)
        wx.MilliSleep(500)
    dialog.Destroy()


def check(path,imagelist):
    app = wx.PySimpleApp()
    frame=wx.Frame(None,-1,"Test",size=(250,150))
    panel=wx.Panel(frame,-1)
    label=wx.StaticText(panel,-1,"What to do next?",(35,20))
    button=wx.Button(panel,-1,"Apply",(90,90))
    gray=wx.CheckBox(panel,-1,"Gray",(35,40),(150,30))

def onclick(event):
    if gray.GetValue():
        grayscale_progress(path,imagelist) 
        frame.Destroy()

button.Bind(wx.EVT_BUTTON,onclick)

frame.Show()
app.MainLoop()

The first problem i see is

button.Bind(wx.EVT_BUTTON,onclick)

The first problem with that line is that if you want to bind a button to an event when the button is clicked you use the event wx.EVT_LEFT_DOWN. This will now call the event when you click down.

The second issue i see is the fact that the variable button is made inside of a function and you are accessing it from outside of that function. This is an issue with scope of a variable. In this case i would recommend that you either make the variable button outside of any functions or classes, making it global. Or you could add global button to the start of the function check.

This is not the only place where we get this issue. It is the same with a lot of the variables like frame, panel, label and gray.

The other thing i see is that you are making two PySimpleApps. Firstly it is best just to use wx.App, it does all that you need now with more functionality. Making two apps just will confuse the program.

I dont know, this might not be your full code, but if it is there are a few more basic problems with it then just even binding.

Hope it helps you though.
Paul

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.