tkinter Button does not open new window - python-3.x

I’m experiencing problems with tkinter.
I have code that is meant to open a new window on button press, but the window does not open.
Here’s my code:
Main Module
#!/usr/bin/python
#encoding: latin-1
import tkinter
import ce
#window config
window = tkinter.Tk() #create window
window.title("BBDOassist") #set title
window.geometry("750x500") #set size
…
# buttons
button_ce = tkinter.Button(window, text="CE Evaluation", command="ce.run()")
button_ce.pack()
window.mainloop() #draw the window and start
’CE’ Module
#!/usr/bin/python
#encoding: latin-1
import tkinter
…
def run():
#window config
window = tkinter.Tk() #create window
window.title("BBDOassist - CE Evaluation") #set title
window.geometry("750x500") #set size
…
window.mainloop() #draw the window and start

You have at least two problems
First, you must give the command attribute a reference to a function. You are passing it a string. A string is useless. You need to change your button definition to this:
button_ce = tkinter.Button(window, text="CE Evaluation", command=ce.run)
Second, if you want to create additional windows then you need to create instances of Toplevel rather than Tk. A tkinter program needs exactly one instance of Tk, and you need to call mainloop exactly once.
Change run to look like this (and remove the call to mainloop inside run):
def run():
#window config
window = tkinter.Toplevel()
...

Related

Calling functions without closing the master window - Python Tkinter

I've tried to create a software. In this software there is a menu widget with function button that open functions e widgets. But, I've noticed, to keep going on script, it's necessary to close the master window (menu).
I've created an example to you understand my problem.
from tkinter import *
#Create Fuction that open new fuctions and widget
def test():
#Open a new widget
def fun_test_1():
top_level = Tk()
def test1():
top_level1 = Toplevel()
top_level1.title('new1')
top_level1.mainloop()
Button(top_level, text='test1',command=test1).pack()
top_level.mainloop()
fun_test_1()
#Before, open the second widget
def fun_test_2():
print('def fun_test_2(): works!')
top_level = Tk()
def test1():
top_level1 = Toplevel()
top_level1.title('new1')
top_level1.mainloop()
Button(top_level, text='Button', command=test1).pack()
top_level.mainloop()
fun_test_2()
root = Tk()
root.title('MASTER')
Button(root, text='Button',command=test).pack()
root.mainloop()
So, I need that fun_test_2() be called without close the root widget
And all functions i've tried to change Tk() to Toplevel() and Toplevel() to Tk().
The problem is you calling mainloop more than once, and for creating more than one instance of Tk. When you call mainloop, it won't return until that window has been destroyed. That's a fundamental aspect of how tkinter works.
The solution is to not create more than one instance of Tk and to not call mainloop more than once. If you need multiple windows, create instances of Toplevel. And again, only call mainloop once in total, not once per window.

how to display a timer in python

hey i am making a program which records desktop screen.
So what i want to do is when ever i click on my start button(tkinter Gui) in my gui window.
It should start a timer like 3.... ,2.... ,1.... in big font directly on my desktop screen and not on my tkinter window. and then my function should start.
How can i do that ..
import tkinter as tk
from tkinter import *
root = Tk()
root.title("our program")
start_cap =tk.button(text='start recording' command=start_capute)
start_cap.pack()
root.mainloop()
Not mentioning the functions and the entire code here as not necessary the code is working fine and i just want to add a new feature of the timer in it.
An minimal example:
import tkinter as tk
# from tkinter import *
def Start():
def Count(Number):
if Number == -1:
win.withdraw()
print("Start") # what you want to do
return False
NumberLabel["text"] = Number
win.after(1000,Count,Number-1)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
win = tk.Toplevel()
win.geometry("+%d+%d"%((screen_width-win.winfo_width())/2,(screen_height-win.winfo_height())/2)) # make it in the center.
win.overrideredirect(1)
win.wm_attributes('-topmost',1) # top window
win.wm_attributes('-transparentcolor',win['bg']) # background transparent.
NumberLabel = tk.Label(win,font=("",40,"bold"),fg='white')
NumberLabel.pack()
win.after(0,lambda :Count(3))
win.mainloop()
root = tk.Tk()
root.title("our program")
start_cap = tk.Button(text='start recording',command=Start)
start_cap.pack()
root.mainloop()

Keyboard event inside a class not working in tkinter

I am trying to capture a keypress after a canvas object is displayed in order to delete the latter, and it's not working. The canvas is displayed properly, but the keypress event is not getting captured. I am a total newbie at Python and this is just a test-code to check my understanding of Tkinter so far. I'm sure it's something fairly basic that I'm missing, so thank you for your patience.
from tkinter import *
class Main:
def __init__(self,master):
# create splash screen
splash = Canvas(master, bg='white')
splash.bind("<Key>",self.splash_key)
splash.pack(fill="both", expand=True)
def splash_key(event):
print('key captured!')
splash.delete("all")
root = Tk()
root.wm_title('Test')
root.attributes('-zoomed',True)
app = Main(root)
root.mainloop()

hiding tkinter window using withdraw vs wm_withdraw

What is the difference between withdraw and wm_withdraw?
import time
import tkinter as tk
def hide():
root.withdraw()
time.sleep(2)
root.deiconify()
root = tk.Tk()
tk.Button(root, text = 'hide', command = hide).pack()
root.mainloop()
When the 'hide' button is clicked, the window is hidden. It disappears from the panel (taskbar), and is not visible in the task view (simultaneous view of all open windows) for 2 seconds.
import time
import tkinter as tk
def hide():
root.wm_withdraw()
time.sleep(2)
root.deiconify()
root = tk.Tk()
tk.Button(root, text = 'hide', command = hide).pack()
root.mainloop()
Same code, but wm_withdraw instead of withdraw. Again, clicking the 'hide' button makes the both the taskbar entry and the window itself invisible for 2 seconds.
Is there any difference at all between these two? Which one should I use? Further, should I use deiconify or wm_deiconify? All four combinations (withdraw, deiconify; wm_withdraw, deiconify; withdraw, wm_deiconify; wm_withdraw, wm_deiconify) seem to do the exact same thing. Is there any application where they will do different things?
There's no difference between them - they both (withdraw and deiconify) just shortucts for wm_ counterparts.
The same applies to all functions, that interact with Window manager under Wm class.
There is no difference between withdraw and wm_withdraw. I can not specify why this was done, but here is the source of tkinter in which we have line withdraw = wm_withdraw (which makes it clear that both calls end up at the same method):
def wm_withdraw(self):
"""Withdraw this widget from the screen such that it is unmapped
and forgotten by the window manager. Re-draw it with wm_deiconify."""
return self.tk.call('wm', 'withdraw', self._w)
withdraw = wm_withdraw

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Resources