Python GUI and Class Structure -


would structure have "root" or "app" , mainloop in class frame instead of seperate , if "if __name.." call?

example:

class app:      def __init__(self):         self.root = tkinter.tk()         self.root.title("color send arduino") 

a reason put app code in class inherits frame makes code more reusable. can insert tkinter gui root without hassle.

for example in app.py

import tkinter tk  class app(tk.frame):     def __init__(self,parent):         tk.frame.__init__(parent)         self.parent = parent      def initialise(self):         pass 

and in other_app.py

from app import app import tkinter tk  if __name__ == "__main__":     root = tk.tk()     myapp = app(root)     myapp.pack()     root.mainloop() 

edit: go along comment below, actual (albeit extremely simple) reusable app clock.

import tkinter tk import getpass import time  class welcomeclock(tk.frame):     def __init__(self,parent):         tk.frame.__init__(self,parent)         self.parent = parent         self.timevar = tk.stringvar()         self.__set_time()         self.initialize()      def initialize(self):         message = "welcome %s!\nthe time currently"%(getpass.getuser().capitalize())         self.welcome = tk.label(self,text=message)         self.welcome.pack(anchor=tk.n)         self.clock = tk.label(self,textvar=self.timevar)         self.clock.pack()      def __set_time(self):         self.timevar.set(time.strftime("%y-%m-%d %h:%m:%s"))         self.after(499,self.__set_time)  if __name__ == "__main__":     root = tk.tk()     clock = welcomeclock(root)     clock.pack()     root.mainloop() 

as welcomeclock class inherits tk.frame can import , pack other tkinter gui use.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -