[Tkinter] newbie with python and tkinter - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] newbie with python and tkinter (/thread-11164.html) |
newbie with python and tkinter - pat - Jun-26-2018 I just begin with python and i am training with tkinter. And i made this training script. My OS is Windows 7 an python version 2.71 I have make a class that create a progress bar and mask it or destroy it. button 1 create progress bar without increment "run" button 2 increment bar "+++" button 3 is an auto increment "update" button 4 hide or destroy (line is in comment) "kill" button 5 exit "quit" If I clic in that order , every thing seem OK. But if I clic "update" first without clic "run" first, then I call this in this def , it don t work. I got this error; Quote:Exception in Tkinter callback I put some global, but it is not the problem... Can you explain me why ? And if you see other errors too ? thanks ! from Tkinter import * import sys import ttk global widget global root global p global running global progress running = 0 root = Tk() def quit(): sys.exit() class progress: global p global widget def __init__(self): self.val = 4 self.counter = 0 running = 0 print self.val print self.counter print running def creat(self): global running running = 1 print running self.widget = ttk.Progressbar(root, orient="horizontal", length=200, mode="determinate") self.widget.pack() def SetVal(self, val): self.val = val def plus(self) : global counter self.counter += self.val print self.counter self.widget.step(self.val) def kill (self): global running running=0 self.counter = 0 # self.widget.destroy() self.widget.pack_forget() def update(delay=50): global running global p global progress global widget if running==0: p.__init__ p.creat p.widget.pack() if p.counter < 98: p.plus() p.widget.after(delay, update) else: p.counter = 0 p.kill() p = progress() p.counter = 0 p.val = 4 while True: Button(root, text="run", command=p.creat).pack() Button(root, text="+++", command=p.plus).pack() Button(root, text="update", command=update).pack() Button(root, text="kill", command=p.kill).pack() Button(root, text="Quit", command=quit).pack() root.mainloop() RE: newbie with python and tkinter - wuf - Jun-26-2018 Hi pat Try to rewrite your script: a) using instead of from Tkinter import * -> import Tkinter as tk b) without any global statement c) the class name starting with a capital letter d) forget the following construct: while True: Button(root, text="run", command=p.creat).pack() Button(root, text="+++", command=p.plus).pack() Button(root, text="update", command=update).pack() Button(root, text="kill", command=p.kill).pack() Button(root, text="Quit", command=quit).pack() root.mainloop()wuf ;-) RE: newbie with python and tkinter - pat - Jun-26-2018 with this "import Tkinter as tk" root = tk() module is not callable if I delete "root = tk()" I have "button is note define" Quote:d) forget the following construct: Ok I replace with what ? RE: newbie with python and tkinter - wuf - Jun-26-2018 Hi pad One way to start: # -*- coding: utf-8 -*- try: import Tkinter as tk import ttk except ImportError: import tkinter as tk from tkinter import ttk APP_TITLE = "My App" APP_XPOS = 100 APP_YPOS = 100 APP_WIDTH = 300 APP_HEIGHT = 200 class Progress(object): def __init__(self, my_app): self.my_app = my_app def creat(self): print("create") def plus(self) : print("plus") def kill (self): print("kill") class MyBeginnersApp(object): def __init__(self, app_win): self.app_win = app_win self.main_frame = tk.Frame(app_win) self.main_frame.pack(fill='both', expand=True, padx=10, pady=10) self.progress = Progress(self) self.build() def build(self): button_frame = tk.Frame(self.main_frame) button_frame.pack(expand=True, padx=4, pady=4) tk.Button(button_frame, text="run", command=self.progress.creat).pack(fill='x') tk.Button(button_frame, text="+++", command=self.progress.plus).pack(fill='x') tk.Button(button_frame, text="update", command=self.update).pack(fill='x') tk.Button(button_frame, text="kill", command=self.progress.kill).pack(fill='x') tk.Button(button_frame, text="Quit", command=quit).pack(fill='x') def update(self): print("update") def main(): app_win = tk.Tk() app_win.title(APP_TITLE) app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS)) #app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT)) app = MyBeginnersApp(app_win) app_win.mainloop() if __name__ == '__main__': main()wuf ;-) RE: newbie with python and tkinter - pat - Jun-26-2018 First, thanks for your help ! I take your sample And add this line; def creat(self): print("create") self.widget = ttk.Progressbar( self.my_app , orient="horizontal", length=200, mode="determinate")And this error occur Quote:C:\Python27>python e:\python35\job\close2.py Now I make a global ROOT def main(): global ROOT app_win = tk.Tk() ROOT = app_winAnd call directly with this global; self.widget = ttk.Progressbar( ROOT , orient="horizontal", length=200, mode="determinate")No Error ! But no progress bar too ! Just the print work. please help. RE: newbie with python and tkinter - wuf - Jun-26-2018 Hi pat Try the following: def creat(self): print("create") self.widget = ttk.Progressbar(self.my_app.main_frame , orient="horizontal", length=200, mode="determinate") self.widget.pack(pady=4)wuf ;-) RE: newbie with python and tkinter - pat - Jun-26-2018 I tested . No error but no progress bar appear. Maybe the version 2.7.14 ? RE: newbie with python and tkinter - wuf - Jun-26-2018 Hi pat Here the progressbar shows up with Python 2.7 & 3.5 with Ubuntu 16.04. Please show your script. wuf ;-) RE: newbie with python and tkinter - pat - Jun-26-2018 I got same idea in the same time ... The progress bar appear with Ubuntu I continue tomorrow. Perhaps others questions thanks |