creating a loading bar?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darktemp
    New Member
    • Feb 2010
    • 25

    creating a loading bar?

    I've always loved how McAfee made a boot time loading bar that basically looks like a spinning bar of /, |, \, -, /, | iterated over and over. Unfortunately I have no idea how to delete things that are already printed in console. Anyone have any ideas?

    I appreciate the help :].
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    You could try using the backspace escape sequence \b (note that this won't work in IDLE, in case you want to experiment with it).

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      I don't know how to do it in a console, but here's a way to cycle characters in a GUI that responds to events:
      Code:
      from Tkinter import *
      from itertools import cycle
      
      class SpinLabel(Frame):
          def __init__(self, master=None):
              Frame.__init__(self, master)
              self.pack(fill=BOTH, expand=1)
              self.master.title("Spin Characters")
              self.textList = ["/", "--", "\\", "--", "|"]
              buttonFrame = Frame(self)
              self.btnList = []
              for i, name in enumerate(["Start", "Stop", "Exit"]):
                  btn = Button(buttonFrame, text=name, padx=5, pady=5,
                               bd=4, bg='#ff0', fg="#00f",
                               activebackground = "#00f",
                               activeforeground = "#ff0",
                               font=("Arial", 12, "bold"),
                               relief='raised')
                  btn.pack(side="left", fill=BOTH, expand=1)
                           
                  def handler(event, i=i):
                      return self.manage_spin(i)
                  btn.bind("<ButtonRelease-1>", handler)
                  self.btnList.append(btn)
              buttonFrame.pack(fill=BOTH, expand=1)
              
              self.spin = Label(self, font=("Tahoma", 24, 'bold'),fg='#000')
              self.spin.pack(side="top", fill=BOTH, expand=1)
              self.spinChrs = cycle(self.textList)
      
          def manage_spin(self, idx):
              if idx == 0:
                  self.btnList[0].configure(state=DISABLED)
                  self.spinning = True
                  self.looping()
              elif idx == 1:
                  self.btnList[0].configure(state=NORMAL)
                  self.spinning = False
                  self.spin.config(text="")
              elif idx == 2:
                  self.master.destroy()
      
          def looping(self):
              if self.spinning:
                  self.spin.config(text=self.spinChrs.next())
                  self.spin.update_idletasks()
                  self.spin.after(100, self.looping)
      
      if __name__ == "__main__":
          app = SpinLabel()
          app.mainloop()

      Comment

      Working...