Python Forum
[Tkinter] newbie with python and tkinter
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] newbie with python and tkinter
#1
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
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "e:\python35\job\close.py", line 56, in update
p.widget.pack()
AttributeError: progress instance has no attribute 'widget'

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()
Reply
#2
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 ;-)
Reply
#3
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 ?
Reply
#4
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 ;-)
Reply
#5
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
create
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "e:\python35\job\close2.py", line 23, in creat
self.widget = ttk.Progressbar( self.my_app , orient="horizontal", length=2
00, mode="determinate")
File "C:\Python27\lib\lib-tk\ttk.py", line 1010, in __init__
Widget.__init__(self, master, "ttk::progressbar", kw)
File "C:\Python27\lib\lib-tk\ttk.py", line 555, in __init__
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2087, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2065, in _setup
self.tk = master.tk
AttributeError: 'MyBeginnersApp' object has no attribute 'tk'

C:\Python27>

Now I make a global ROOT

def main():
	global ROOT
	app_win = tk.Tk()
	ROOT = app_win
And 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.
Reply
#6
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 ;-)
Reply
#7
I tested .
No error but no progress bar appear.

Maybe the version 2.7.14 ?
Reply
#8
Hi pat

Here the progressbar shows up with Python 2.7 & 3.5 with Ubuntu 16.04.

Please show your script.

wuf ;-)
Reply
#9
I got same idea in the same time ...
The progress bar appear with Ubuntu

I continue tomorrow.
Perhaps others questions

thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Python Newbie MartyH 6 2,703 May-13-2020, 07:07 PM
Last Post: MartyH
  Newbie question with Tkinter Entry mariolopes 2 2,201 Oct-12-2019, 11:02 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020