Fortschreitende Progressbar mit wxPython

Code-Stücke können hier veröffentlicht werden.
Antworten
DaSch
User
Beiträge: 72
Registriert: Mittwoch 28. März 2007, 20:04
Kontaktdaten:

Sonntag 1. April 2007, 21:46

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.4.1 on Sun Apr  1 22:37:09 2007

import wx
import time

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.button_start = wx.Button(self, 100, "Zeige Progressbar")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.ON_START, id=100)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MainFrame.__set_properties
        self.SetTitle("Main")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        sizer_main = wx.BoxSizer(wx.VERTICAL)
        sizer_main.Add(self.button_start, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 15)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_main)
        sizer_main.Fit(self)
        sizer_main.SetSizeHints(self)
        self.Layout()
        # end wxGlade

    def ON_START(self, event): # wxGlade: MainFrame.<event_handler>
        #Code für das Fortschreiten der Progressbar
        start_progress = Progress(self)
        start_progress.Show()
        start_progressbar = start_progress.progressbar
        for i in range(101):
            start_progressbar.SetValue(i)
            wx.Dialog.RequestUserAttention(start_progress)
            time.sleep(0.1)

# end of class MainFrame


class Progress(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Progress.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.sizer_progress_staticbox = wx.StaticBox(self, -1, "Progress")
        self.progressbar = wx.Gauge(self, -1, 100)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Progress.__set_properties
        self.SetTitle("Progress")
        self.SetSize((500, 70))
        self.progressbar.SetMinSize((480, 28))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Progress.__do_layout
        sizer_progress = wx.StaticBoxSizer(self.sizer_progress_staticbox, wx.VERTICAL)
        sizer_progress.Add(self.progressbar, 0, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 5)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_progress)
        sizer_progress.Fit(self)
        sizer_progress.SetSizeHints(self)
        self.Layout()
        # end wxGlade

# end of class Progress


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    mainframe = MainFrame(None, -1, "")
    app.SetTopWindow(mainframe)
    mainframe.Show()
    app.MainLoop()
Mal so eine kleine Progressbar die sich langsam aber Stetig vorarbeitet.

In die Schleife kann man dann für Bestimmte schritte dann bestimmte funktionen einbauen
DaSch
User
Beiträge: 72
Registriert: Mittwoch 28. März 2007, 20:04
Kontaktdaten:

Montag 2. April 2007, 10:12

Also ich hab des nun ein wenig überarbeitet allerdings bewegt sich die Progressbar nicht, weiß jemand vllt wieso?

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# generated by wxGlade 0.4.1 on Sun Apr  1 22:37:09 2007

import wx
import time
import thread

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.button_start = wx.Button(self, 110, "Zeige Progressbar")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.ON_START, id=110)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MainFrame.__set_properties
        self.SetTitle("Main")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        sizer_main = wx.BoxSizer(wx.VERTICAL)
        sizer_main.Add(self.button_start, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 15)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_main)
        sizer_main.Fit(self)
        sizer_main.SetSizeHints(self)
        self.Layout()
        # end wxGlade

    def ON_START(self, event): # wxGlade: MainFrame.<event_handler>
        #Starten des Haupt Threads
        print "Event start"
        prog=Progress(self)
        print type(prog)
        print prog
        prog.execution()
        print "new Thread started"

# end of class MainFrame


class Progress(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: Progress.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.sizer_progress_staticbox = wx.StaticBox(self, -1, "Progress")
        self.progressbar = wx.Gauge(self, 210, 100)
        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: Progress.__set_properties
        self.SetTitle("Progress")
        self.SetSize((500, 70))
        self.progressbar.SetMinSize((480, 28))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: Progress.__do_layout
        sizer_progress = wx.StaticBoxSizer(self.sizer_progress_staticbox, wx.VERTICAL)
        sizer_progress.Add(self.progressbar, 0, wx.ALL|wx.EXPAND, 5)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_progress)
        self.Layout()
        # end wxGlade
    
    def go(self,start,end):
        #der Progressbar Thread
        for i in range(start,end):
            print i
            self.progressbar.SetValue(i+1)
            wx.Dialog.RequestUserAttention(self)
            wx.SafeYield(self.progressbar)
            time.sleep(0.1)
        print "Progressbar Ende beendet"
    
    def execution(self):
        #Der Kommando Thread
        print "Thread Start"
        self.Show()
        wx.Dialog.RequestUserAttention(self)
        step = 10
        start = 0
        end = start + step
        msg = (start,end)
        
        def stepping(msg,start,end,step):
            #Stepping
            start += step
            end += step
            msg = (start,end)
            return msg,start,end
        
        for i in range(step):
            thread.start_new_thread(self.go,msg)
            msg,start,end = stepping(msg,start,end,step)
            if i == 0:
                time.sleep(3)#erstes Commando
                print "Command 1 exec done"
            elif i == 1:
                time.sleep(3)#zweites Commando
                print "Command 2 exec done"
            elif i == 2:
                time.sleep(3)#drittes Commando
                print "Command 3 exec done"
            elif i == 3:
                time.sleep(3)#viertes Commando
                print "Command 4 exec done"
            elif i == 4:
                time.sleep(3)#fünftes Commando
                print "Command 5 exec done"
            elif i == 5:
                time.sleep(3)#sechstes Commando
                print "Command 6 exec done"
            elif i == 6:
                time.sleep(3)#siebtes Commando
                print "Command 7 exec done"
            elif i == 7:
                time.sleep(3)#achtes Commando
                print "Command 8 exec done"
            elif i == 8:
                time.sleep(3)#neuntes Commando
                print "Command 9 exec done"
            else:
                print "End of Command"
        print "End of Progress"
# end of class Progress


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    mainframe = MainFrame(None, -1, "")
    app.SetTopWindow(mainframe)
    mainframe.Show()
    app.MainLoop()

Danke für eure Hilfe
mmg
User
Beiträge: 11
Registriert: Dienstag 15. Mai 2007, 14:47

Dienstag 15. Mai 2007, 14:51

Warum eigentlich so komplex?

http://wxpython.org/docs/api/wx.Progres ... class.html

Code: Alles auswählen

import wx

a = wx.App(0)
dialog = wx.ProgressDialog(titel, beschriftung, maximalwert, parent_objekt, wx_style)
dialog.Show(True)

for aktuellerwert in range(100):
    dialog.Update(aktuellerwert, newmsg="NeueBeschriftung")
dialog.Destroy() 
grüße,
marco
Antworten