in my code, the main window is emitting event signals, which are to be caught by the child windows, which will show the change in a label. But only the last child window catches the event signal, and changes it's label. what's wrong?
from Tkinter import *
from threading import Timer as tt
class main(Tk):
def __init__(self):
Tk.__init__(self)
tt(.5,self.timedsig).start()
for i in range (5):
child(self,i)
def timedsig(self):
self.event_generate("<<timedsig>>")
tt(.5,self.timedsig).start()
class child(Toplevel):
def __init__(self,master,num):
Toplevel.__init__(self)
self.title(str(num))
self.num=num
self.var=IntVar()
self.var.set(0)
Label(self,textvariable=self.var).pack()
self.master=master
self.master.bind("<<timedsig>>",self.changelabel)
def changelabel(self,e):
print self.num,self.var.get()
self.var.set(self.var.get()+1)
if __name__=="__main__":
main().mainloop()
Calling bind also unbinds all previous functions. To bind an additional function, you need to use the optional 3rd argument:
self.master.bind("<<timedsig>>",self.changelabel, '+')
But that's pretty silly to do in your case when you can simply pass the variable to the instances:
import Tkinter as tk
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.var = tk.IntVar(value=0)
for i in range (5):
Popup(self, i, self.var)
self.timedsig() # start the looping
def timedsig(self):
self.var.set(self.var.get() + 1)
self.after(500, self.timedsig) # call this again in 500 ms
class Popup(tk.Toplevel):
def __init__(self, master, num, var):
tk.Toplevel.__init__(self)
self.title(str(num))
lbl = tk.Label(self,textvariable=var)
lbl.pack()
if __name__=="__main__":
root = Main()
root.mainloop()
Also, that's not what we usually call a "child". It's just a different object.
Related
This attempt is try to get the value from a function in class One which is retrieve when the function is call. Then the value [string1] would be passed as a parameter for class Two's method call which's within tkinter, any idea on how can it be attain
from tkinter import Tk, Label, Button
from tkinter import *
from tkinter.ttk import *
class One:
def __init__(self, master):
self.master = master
master.title("Test")
self.greet_button = Button(master, text="Test", command=self.do)
self.greet_button.pack()
def do(self):
...some action...
string1='test'
return string1
class Two:
def write(self, str):
...some action...
#object instantiate within tkinter
root = Tk()
p0 = One(root)
p0.do()
p1 = Two()
p1.write(string1)
root.mainloop()
##from tkinter import Tk, Label, Button
##
##from tkinter import *
##from tkinter.ttk import *
####why are you importing some and then everything?
import tkinter as tk
class Reader_Class(tk.Frame):
def __init__(self, master, writer_class):
tk.Frame.__init__(self,master)
self.master = master
master.title("Test")
self.writer = writer_class
self.greet_button = tk.Button(self, text="Test", command=self.do)
self.greet_button.pack()
def do(self):
string1='test'
self.writer.write(string1)
class Writer_Class(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
def write(self, test):
print(test)
root = tk.Tk()
writer = Writer_Class(root)
writer.pack()
reader = Reader_Class(root, writer)
reader.pack()
root.mainloop()
Explaination:
So first of all, it isnt necessary to import stuff twice.
Second you need to pass through the interface a reference, therefor you need to have reference. Thats why it makes sense to use the construktor first on the writer_class.
Next you give the reference as a parameter to the reader_class.
reader = Reader_Class(root, writer)
In the reader_class you keep a reference in the local_scope by using the syntax self..
self.writer = writer_class
Finally you use the reference of that class and calling a method of this class, where you can pass through a parameter of your choice.
self.writer.write(string1)
I am trying to inherit some values from one class to a another one. I am using the function super to inherit. Below is a simplfied version of my problem. Thank you for help.
from tkinter import *
import random
class First(object):
def __init__(self,master):
super(First, self).__init__(master)
def random(self):
self._y = random.randint(11,20)
self._x = random.randint(1,10)
def random2(self):
s = First(root)
s.random()
class Second(Frame,First):
def __init__(self,master):
super(Second, self).__init__(master)
self.grid()
self.menuFrame = Frame(self)
self.create_menu_widgets()
self.menuFrame.grid()
def create_menu_widgets(self):
btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
btnMainMenu.pack()
def print(self):
print(self._y,self._x)
root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background = 'green')
root.mainloop()
I keep on getting the error:
super(First, self).__init__(master)
TypeError: object.__init__() takes no parameters
Please help me, I think the problem is where I have s=First(root). Thanks for help.
When you call super on a class that is the highest in your hierarchy it will go object. object is the super class of all objects in Python. So super(First, self).__init__(master) will try to initialize the object not any of your classes. You can see this inheritance using the Class.__mro__. To figure out what I'm talking about.
And inheriting from object? That happens by default even if you don't specify anything. So I guess you wanted to inherit from Frame as object doesn't make any sense.
So change your code to this and it should be fixed.
from tkinter import *
import random
class First(Frame): # changed here
def random(self):
self._y = random.randint(11,20)
self._x = random.randint(1,10)
def random2(self):
s = First(root)
s.random()
class Second(First): # changed here
def __init__(self,master):
super(Second, self).__init__(master)
self.grid()
self.menuFrame = Frame(self)
self.create_menu_widgets()
self.menuFrame.grid()
def create_menu_widgets(self):
btnMainMenu = Button(self.menuFrame,font=("consolas",18,"bold"),text="Main Menu")
btnMainMenu.pack()
def print(self):
print(self._y,self._x)
root = Tk()
x = Second(root)
x.random()
x.random2()
x.print()
root.configure(background = 'green') # you cannot see this as your button fills everything
root.mainloop()
I see several issues in your example.
1:
you are assigning Second() to x but then calling x.random() and x.random2(). This will not work as your random methods only exist in the First() class.
2:
Don't name a function, method, variable or attribute the same thing as a built in method. This will cause problems.
Change your def print(self) to something like def my_print(self) or anything that is not exactly print. While we are talking about this print statement you only define self._x and self._y in your First() class but try to print them in your Second() class. This will never work. self is always a reference to the class object and never a reference to a class controller that was passed to the class.
Now I get what you are trying to do here and I will rebuild your code to show how to share information between classes.
You should not use a geometry manager fro inside the Frame class. Instead use it on the class variable name. This will allow you chose between any geometry manager for the class instead of sticking to just one kind.
As Vineeth pointed out you do not use supper for an object class.
The below code will run the Second() class and then when you want to reference the random methods on the First() class you can do so with the new methods I added to Second(). Let me know if you have any questions.
One last change is to import tkinter as tk this will help prevent accidentally overwriting an imported method from tkinter.
Here is a working example of your code:
import tkinter as tk
import random
class First(object):
def random(self):
return "From First.Random!", random.randint(11,20), random.randint(1,10)
def random2(self):
return "From First.Random2!", self.random()
class Second(tk.Frame):
def __init__(self, master):
super(Second, self).__init__(master)
self.menuFrame = tk.Frame(self)
self.menuFrame.grid()
tk.Button(self.menuFrame, font=("consolas", 18, "bold"), text="Main Menu").pack()
def random(self):
print(First().random())
def random2(self):
print(First().random2())
root = tk.Tk()
root.configure(background='green')
x = Second(root)
x.pack()
x.random()
x.random2()
root.mainloop()
I have a Tkinter program which I want to pause for 3 seconds.
time.sleep doesn't work and the after method doesn't do exactly what I want to.
here is an example code:
from Tkinter import *
def waithere():
print "waiting..."
root = Tk()
print "1"
root.after(3000,waithere)
print "2"
root.mainloop()
output:
1
2
*3 seconds*
waiting...
the output i want to have:
1
waiting...
*3 seconds*
2
thanks.
Normally it's a very bad idea to have a GUI wait for something. That's imply not how event-based programs work. Or more accurately, GUIs are already in a perpetual wait state, and you don't want to block that with your own waiting.
That being said, tkinter has a way to wait until certain things happen. For example, you can use one of the "wait" functions, such as wait_variable, wait_window, or wait_visibility.
Assuming that you wanted waithere to do the waiting, you could use wait_variable to do the waiting, and after to set the variable after a given amount of time.
Here's the solution based on your original code:
from Tkinter import *
def waithere():
var = IntVar()
root.after(3000, var.set, 1)
print("waiting...")
root.wait_variable(var)
root = Tk()
print "1"
waithere()
print "2"
root.mainloop()
The advantage to using these methods is that your code is still able to respond to events while it is waiting.
I found a way like that, i hope it helps you:
from tkinter import *
def waitToShow():
index = 1
while index < 11:
l1.config(text=index)
l1.after(1000)
l1.update()
index += 1
win = Tk()
l1 = Label(win)
l1.pack()
waitToShow()
win.mainloop()
Just for future reference, refrain from using long or infinite loops in Tkinter; they will prevent the UI from responding to user events (AKA freezing). The method I was taught was to periodically update the field using the after() function.
The after() function creates an alarm-callback meaning when called (with the right parameters) it will queue a call to the target method (in the example below def update(self) with our entered delay. You can use a boolean in the class to exit the loop. Create on on __init__ and then when set to False don't call after() anymore.
Here is an example creating a class inheriting Tkinter.Frame to inherit the functionality.
try:
import tkinter as tk
except:
import Tkinter as tk
import datetime
class DelayedUpdateUI(tk.Frame):
def __init__(self, master=None, **kw):
# Create widgets, if any.
tk.Frame.__init__(self, master=master, **kw)
self.timeStr = tk.StringVar()
self.lblTime = tk.Label(self, textvariable=self.timeStr)
self.lblTime.grid()
# Call update to begin our recursive loop.
self.update()
def update(self):
self.timeStr.set(datetime.datetime.now())
# We use after( milliseconds, method_target ) to call our update
# method again after our entered delay. :)
self.after(1000, self.update)
if __name__ == '__main__':
root = tk.Tk()
DelayedUpdateUI(root).grid()
root.mainloop()
A suggestion based on Bryan's answer:
I understand the recommended way from an event-based perspective, but it does not feel very intuitive to me. I have to look up the trick every time I need it. Therefore, I have created a small mixin class that makes usage a bit more intuitive:
import tkinter as tk
class TkWaitMixin:
"""Simple wait timer that makes Tk waiting functionality
more intiutive. Applies the recommended way according to
https://stackoverflow.com/a/51770561/12646289.
"""
def start_wait_timer(self, milliseconds):
self.resume = tk.BooleanVar(value=False)
self.master.after(milliseconds, self.resume.set, True)
# Assume master attribute is available:
# https://stackoverflow.com/a/53595036/12646289
def wait_on_timer(self):
self.master.wait_variable(self.resume)
Example usage:
import tkinter as tk
class MyWindow(tk.Tk, TkWaitMixin):
def __init__(self, master):
self.master = master
self.message_label = tk.Label('')
self.message_label.pack(padx=50, pady=50)
def show_message(self, message, milliseconds):
self.start_wait_timer(milliseconds)
self.message_label['text'] = message
self.wait_on_timer()
self.message_label['text'] = ''
root = tk.Tk()
mywin = MyWindow(master=root)
mywin.show_message('Hello world', 2000)
root.mainloop()
Obviously, this will only be of use if you use classes in your tkinter code. Also note that the master attribute should be available in the main class to which the mixin is added.
Edit
Alternatively, usage can be made even easier with a context manager:
import tkinter as tk
class TkWait:
def __init__(self, master, milliseconds):
self.duration = milliseconds
self.master = master
def __enter__(self):
self.resume = tk.BooleanVar(value=False)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.master.after(self.duration, self.resume.set, True)
self.master.wait_variable(self.resume)
Note that the waiting starts when the context manager is exited.
Example usage:
import tkinter as tk
class MyWindow(tk.Tk):
def __init__(self, master):
self.master = master
self.message_label = tk.Label('')
self.message_label.pack(padx=50, pady=50)
def show_message(self, message, milliseconds):
with TkWait(self.master, milliseconds):
self.message_label['text'] = message
self.message_label['text'] = ''
root = tk.Tk()
mywin = MyWindow(master=root)
mywin.show_message('Hello world', 2000)
root.mainloop()
You forgot to do the () at root.after(3000,waithere <<<<-)
from tkinter import *
def waithere():
print("waiting...")
root = Tk()
print("1")
root.after(3000,waithere())
print("2")
root.mainloop()
So I'm making a programming language in Python.
This is the code I have so far:
import tkinter as tk
import tkinter.messagebox as m
import sys
class IOscriptError(Exception):
pass
class Std:
def __init__(self):
self.root = tk.Tk()
self.root.title("STDOUT")
self.stdouttext = [""]
self.outstd = tk.Label(self.root, text=self.stdouttext)
self.outstd.pack()
def out(self, value):
self.stdouttext.append(value + "\n")
self.outstd.config(text=''.join(self.stdouttext))
def start(self):
self.root.mainloop()
std = Std()
class Gui:
def __init__(self):
pass
def newButton(self, val, command="m.showinfo('title', 'message')"):
self.b=tk.Button(std.root, text=val, command=command).pack()
gui = Gui()
std.out("Hello!")
std.out("How are you?")
gui.newButton("Hello!")
std.start()
The problem is that the button gui.b's command is not running.
I've also tried with the use of lambda.
It just doesn't work!
Can you please tell me why this is happening and how to fix it?
Thanks!
The problem is that you are trying to pass a string as the command instead of a function. Instead of command="m.showinfo('title', 'message')", try something like this:
def TestCommand():
m.showinfo('title', 'message')
class Gui:
def __init__(self):
pass
def newButton(self, val, command=TestCommand):
self.b=tk.Button(std.root, text=val, command=command).pack()
Remember that the Button constructor takes a function as the command parameter and not a string.
There is a main window with menu and the progressbar. A correspondence window with OK button opens upon menu command and the OK button starts the process (here: 3 sec. sleep).
The correspondence window is created via inheritance from a class I have not provided here (If required for answer, please let me know). The methods apply and ok override existing methods in the mother class.
Now my problem: Since the progressbar sits in the main window (class App) and progressbar(start) and progressbar(stop) in the correspondence window I somehow have to pass (start) and (stop) via the mother class tkSimpleDialog.Dialog to class App. So I thought I also override the __init__(self..) method, provide self. to progressbar.
How can I make this work?
import Tkinter, ttk, tkFileDialog, tkSimpleDialog, time, threading
class App:
def __init__(self, master, progressbar):
self.progress_line(master)
def progress_line (self, master):
self.progressbar = ttk.Progressbar(master, mode='indeterminate')
self.progressbar.place(anchor = 'ne', height = "20", width = "150", x = "175", y = "30")
class AppMenu(object):
def __init__(self, master, progressbar):
self.master = master
self.menu_bar()
def menu_bar(self):
menu_bar = Tkinter.Menu(self.master)
self.menu_bar = Tkinter.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.create_menu = Tkinter.Menu(self.menu_bar, tearoff = False)
self.create_menu.add_command(label = "do", command = self.do)
self.menu_bar.add_cascade(label = "now", menu = self.create_menu)
def do(self):
do1 = Dialog(self.master, progressbar)
class Dialog(tkSimpleDialog.Dialog):
def __init__(self, parent, progressbar):
tkSimpleDialog.Dialog.__init__(self, parent, progressbar)
self.transient(parent)
self.parent = parent
self.result = None
self.progressbar = progressbar
body = Frame(self)
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
self.buttonbox()
self.grab_set()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50, parent.winfo_rooty()+50))
self.initial_focus.focus_set()
self.wait_window(self)
def ok(self, event=None):
self.withdraw()
self.start_foo_thread()
self.cancel()
def apply(self):
time.sleep(5)
def start_foo_thread(self):
global foo_thread
self.foo_thread = threading.Thread(target=self.apply)
self.foo_thread.daemon = True
self.progressbar.start()
self.foo_thread.start()
master.after(20, check_foo_thread)
def check_foo_thread(self):
if self.foo_thread.is_alive():
root.after(20, self.check_foo_thread)
else:
self.progressbar.stop()
master = Tkinter.Tk()
progressbar = None
app = App(master, progressbar)
appmenu = AppMenu(master, progressbar)
master.mainloop()
error messages:
first after clicking ok:
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
File "ask-progressbar.py", line 57, in ok
self.start_foo_thread()
File "ask-progressbar.py", line 66, in start_foo_thread
self.progressbar.start()
AttributeError: Dialog2 instance has no attribute 'progressbar'
second: after closing app
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1410, in __call__
File "ask-progressbar.py", line 26, in do
do1 = Dialog2(self.master, progressbar)
File "ask-progressbar.py", line 33, in __init__
self.transient(parent)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1652, in wm_transient
TclError: can't invoke "wm" command: application has been destroyed
Below is a working version of your code. There were a number of issues I had to fix because you didn't change a number of things in the code from my answer to your other question about progressbars.
The answer to your main question here is basically that you have to pass the instance around and remember it when necessary in the various class instances involved so that their methods will have it available through theself argument when they need it. Also, the way you were trying to derive and override the tkSimpleDialog.Dialog base class methods was both over-complicated and incorrect as well.
Usually the best (and simplest) thing to do is just supply your own validate() and apply() methods since that's how it was designed to work. If you also need your own __init__() constructor, it's important to only pass parameters to the base class' method that it understands from within the one in the subclass. If you need more functionality, it can usually be provided via additional derived-class-only methods, that only it or other classes you've also created know about.
Anyway, here's what I ended-up with:
import Tkinter, ttk, tkFileDialog, tkSimpleDialog, time, threading
class App:
def __init__(self, master):
self.progress_line(master)
def progress_line(self, master):
# the value of "maximum" determines how fast progressbar moves
self._progressbar = ttk.Progressbar(master, mode='indeterminate',
maximum=4) # speed of progressbar
self._progressbar.place(anchor='ne', height="20", width="150",
x="175", y="30")
#property
def progressbar(self):
return self._progressbar # return value of private member
class AppMenu(object):
def __init__(self, master, progressbar):
self.master = master
self.menu_bar()
self.progressbar = progressbar
def menu_bar(self):
self.menu_bar = Tkinter.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.create_menu = Tkinter.Menu(self.menu_bar, tearoff=False)
self.create_menu.add_command(label="do", command=self.do)
self.menu_bar.add_cascade(label="now", menu=self.create_menu)
def do(self):
Dialog(self.master, self.progressbar) # display the dialog box
class Dialog(tkSimpleDialog.Dialog):
def __init__(self, parent, progressbar):
self.progressbar = progressbar
tkSimpleDialog.Dialog.__init__(self, parent, title="Do foo?")
def apply(self):
self.start_foo_thread()
# added dialog methods...
def start_foo_thread(self):
self.foo_thread = threading.Thread(target=self.foo)
self.foo_thread.daemon = True
self.progressbar.start()
self.foo_thread.start()
master.after(20, self.check_foo_thread)
def check_foo_thread(self):
if self.foo_thread.is_alive():
master.after(20, self.check_foo_thread)
else:
self.progressbar.stop()
def foo(self): # some time-consuming function...
time.sleep(3)
master = Tkinter.Tk()
master.title("Foo runner")
app = App(master)
appmenu = AppMenu(master, app.progressbar)
master.mainloop()
Hope this helps.
Here's another, simpler, solution that doesn't require the use of threading -- so could be easier to use/adapt in your case. It calls the progressbar widget's update_idletasks() method multiple times during the time-consuming foo() function. Again, it illustrates how to pass the progressbar around to the various parts of the code that need it.
import Tkinter, ttk, tkFileDialog, tkSimpleDialog, time
class App:
def __init__(self, master):
self.progress_line(master)
def progress_line(self, master):
self._progressbar = ttk.Progressbar(master, mode='indeterminate')
self._progressbar.place(anchor='ne', height="20", width="150",
x="175", y="30")
#property
def progressbar(self):
return self._progressbar # return value of private member
class AppMenu(object):
def __init__(self, master, progressbar):
self.master = master
self.menu_bar()
self.progressbar = progressbar
def menu_bar(self):
self.menu_bar = Tkinter.Menu(self.master)
self.master.config(menu=self.menu_bar)
self.create_menu = Tkinter.Menu(self.menu_bar, tearoff=False)
self.create_menu.add_command(label="do foo", command=self.do_foo)
self.menu_bar.add_cascade(label="now", menu=self.create_menu)
def do_foo(self):
confirm = ConfirmationDialog(self.master, title="Do foo?")
self.master.update() # needed to completely remove conf dialog
if confirm.choice:
foo(self.progressbar)
class ConfirmationDialog(tkSimpleDialog.Dialog):
def __init__(self, parent, title=None):
self.choice = False
tkSimpleDialog.Dialog.__init__(self, parent, title=title)
def apply(self):
self.choice = True
def foo(progressbar):
progressbar.start()
for _ in range(50):
time.sleep(.1) # simulate some work
progressbar.step(10)
progressbar.update_idletasks()
progressbar.stop()
master = Tkinter.Tk()
master.title("Foo runner")
app = App(master)
appmenu = AppMenu(master, app.progressbar)
master.mainloop()