how to make Tkinter windows appear when opened rather than start minimized? - python

How can I open Tkinter windows (such as entry, text...) and make them appear on the screen when they are opened rather than start minimized ?
I don't really know how to start... I have some windows but they are opened minimized. I searched on the internet, but found nothing that may be relevant. how can I do it ?
using python on windows (both Python 3 and Python 2)
thanks for the help in advance !
EDIT: the problem now as I mentioned in a comment here is that I have to force the window to be showed. But when I do so, the window does not start centered even if I use a function to center it that worked before.
code:
def center(toplevel):
toplevel.update_idletasks()
w = toplevel.winfo_screenwidth()
h = toplevel.winfo_screenheight()
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
x = w/2 - size[0]/2
y = h/2 - size[1]/2
toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))
def paste_func():
global text_box
text_box.insert(END, top.clipboard_get())
button_pressed()
def button_pressed(x=0):
# This function determines which button was pressed, and closes this menu/message box/etc...
global pressed
pressed = x
destroy_top()
def destroy_top():
# This function closes this menu/message box/etc...
global top
top.iconify()
top.withdraw()
top.quit()
def get_text():
global pressed
global top
global text_box
pressed = 0
top = Tk()
top.withdraw()
top.rowconfigure(0, weight=0)
top.columnconfigure(0, weight=0)
top.config(height=0, width=0)
top.protocol('WM_DELETE_WINDOW', lambda: button_pressed(-1))
text_box = Entry(top, width=50)
text_box.focus_set()
text_box.grid(row=0, column=0)
but = Button(top, text='Enter', command=button_pressed)
but.grid(row=0, column=1)
paste = Button(top, text='Paste', command=paste_func)
paste.grid(row=0, column=2)
top.deiconify()
text_box.focus_set()
top.after(0, top.focus_force())
center(top)
top.mainloop()
if pressed == -1:
exit()
return text_box.get('1.0', index2=END)

The window.focus_force() method does this:
Force the input focus to the widget. This is impolite. It's better to wait for the window manager to give you the focus. See also .grab_set_global() below.
Sometimes if this doesn't work, you can manually force it like so:
from Tkinter import *
window = Tk()
window.after(2000, window.focus_force)
window.mainloop()
Sometimes you will have issues on Macs which can require some additional finagling but this should work fine elsewhere (OP has not specified any information about environment).

Related

Mouseover on tkinter button

Is it possible to get the popup option (pop function in the code) on hovering the cursor on the button?
import tkinter as tk
from tkinter import Tk, BOTH, Menu
def pop(bt):
try:
x = bt.winfo_rootx()+238
y = bt.winfo_rooty()+10
popup.tk_popup(x, y, 0)
finally:
popup.grab_release()
root = tk.Tk()
popup = Menu(root, tearoff=0,relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")
button1 =tk.Button(root, text="HELP",height=3,width=26,command=lambda: controller.show_frame(HelpPage))
button1.configure(command = lambda: pop(button1))
button1.place(x=0,y=0
)
root.mainloop()
.
button1.bind('<Enter>',pop(button1)) #gives the following output without the mouse cursor over that button.
Try this:
import tkinter as tk
window = None
def leave_window(event):
global window
if 0 < root.winfo_pointerx() - root.winfo_rootx() < button.winfo_width():
if 0 < root.winfo_pointery() - root.winfo_rooty() < button.winfo_height():
# Mouse still over button
return None
if window is not None:
window.destroy()
window = None
def create_window(event):
global window
if window is not None:
# The window is already open
return None
window = tk.Toplevel(root)
window.overrideredirect(True)
label = tk.Label(window, text="Text", bg="black", fg="white")
label.pack()
window.update()
# Move the window to the cursor's
x = root.winfo_pointerx()
y = root.winfo_pointery()-window.winfo_height()
window.geometry("+%i+%i" % (x, y))
window.bind("<Leave>", leave_window)
root = tk.Tk()
button = tk.Button(root, text="-------- Hover the mouse here --------")
button.pack(fill="both", expand=True)
button.bind("<Enter>", create_window)
button.bind("<Leave>", leave_window)
root.mainloop()
I binded to <Enter> and <Leave> to check if the mouse is over the button. If it is, it creates a window with a label called text. You can change it to it showing the menu.
For more answers look here.
Please bind your Button with mouse enter and call pop function.
Check out this code :
button1.bind('<Enter>', pop)
Control your popup location as well as destroy popup on '' case again bind it and call custom function that will destroy popup.
You need <Enter> and <Leave> bind sequences to map and unmap the Menu. Tkinter Menu has two methods post and unpost where post shows the menu at given coordinates, and unpost hides it away. Unfortunately, I couldn't test it as the unpost functionality doesn't work on macOS or Linux [refer to this link for the same]. I also changed the x, y coords to map the Menu in the center of the widget (Button), it can be changed if required.
Here is the complete sample code.
import tkinter as tk
from tkinter import Tk, BOTH, Menu
def pop(evt):
but = evt.widget
if str(evt.type) == "Enter":
# Map the menu in the center of the width.
x = but.winfo_rootx() + int(but.winfo_width()/2)
y = but.winfo_rooty() + int(but.winfo_height()/2)
popup.tk_popup(x, y)
elif str(evt.type) == "Leave":
popup.unpost()
root = tk.Tk()
root.geometry("300x300")
popup = Menu(root, tearoff=0, relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")
button1 = tk.Button(root, text="HELP", height=3, width=26)
button1.bind('<Enter>', pop)
button1.bind('<Leave>', pop)
button1.pack(pady=100)
root.mainloop()
Like said, unpost doesn't work on macOS or Linux, so I couldn't test the sample code 100% but it should work fine.

Why isn't the label animation working till the last value of the loop?

I am new to python and I have been learning tkinter recently. So I thought with myself that using the grid_forget() function I can remove a widget and redefine it. I thought of this animation that changes the padding of a label so it would create space (kind of like moving the label but not exactly). However, the animation does not work at all. The program freezes until the label reaches the last value of the padding. How can I fix this? Or is there a better way to animate a label moving in the screen?
Here is my code:
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='------')
lbl.grid(row=0, column=0)
def animation():
padding = 0
while padding < 31:
lbl.grid_forget()
padding += 1
lbl.grid(row=0, column=0, padx=padding)
time.sleep(0.2)
# alternative: root.after(200, lambda: lbl.grid(row=0, column=0, padx=padding))
btn = Button(root, text='Animate', command=animation)
btn.grid(row=1, column=1)
root.mainloop()
You need to update the screen for changes to be shown.
Here is a working version using the .update() method:
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='------')
lbl.grid(row=0, column=0)
def animation():
padding = 0
while padding < 31:
lbl.grid_forget()
padding += 1
lbl.grid(row=0, column=0, padx=padding)
root.update()
time.sleep(0.2)
# alternative: root.after(200, lambda: lbl.grid(row=0, column=0, padx=padding))
btn = Button(root, text='Animate', command=animation)
btn.grid(row=1, column=1)
root.mainloop()
Here is a way I also use to animate stuff on the screen, I am not able to understand what you were trying to achieve with your code snippet above, I tried making some changes to it but I feel this way is much better and let's you get more control of your window.
This uses the widely used Canvas widget in the tkinter library.
The Canvas is a general purpose widget, You can use it for a lot of things. Visit the hyper link for more clarity
Here is a short example of how you would create text on the screen.
from tkinter import *
root = Tk()
root.title("My animation")
c = Canvas(root)
x = 20
y = 20 #Instead of using row and column, you simply use x and y co-ordinates
#We will use these co-ordinates to show where the text is in the starting
my_text = c.create_text(x,y,text = '-----')
c.pack()
# This is all you need to create this text on your screen!
root.mainloop()
The idea is that you put your canvas up on your window , and then place whatever you want on it.
There are a lot more attributes that you can add to make your text look even better. Here is an in-depth tutorial on it.
Now that we have made your text widget, It is now time to move it around. Let us move it to 90,20 From our initial position which is 20,20
Here is how we will do it. If we simply move to text object to 90,90, We won't see any animations, it will just directly have it there. So what we will do is first create it at 21,20. Then 22,20. And so on...
We do this really fast till we reach 90,20
This looks like we are moving the text
from tkinter import *
import time
root = Tk()
root.title("My animation")
c = Canvas(root)
x = 20
y = 20 #Instead of using row and column, you simply use x and y co-ordinates
#We will use these co-ordinates to show where the text is in the starting
my_text = c.create_text(x,y,text = 'weee')
c.pack()
def animation():
y = 0.1
x = 0
for _ in range(1000):
c.move(my_text,x,y)
root.update()
anlabel = Button(root,text = 'Animate!',command = animation).pack()
root.mainloop()
This is not only applicable to text, but everything (like other images)that is there on the canvas. The canvas also has Events which will let you use mouse-clicks and other keys on the computer too.
I have made some changes from the previous code, But it is executable and you can try it for yourself to see how it works. increasing the value in time.sleep() makes the animation slower, the lesser the value, the faster.
Are you sure you aren't trying to do something more like the below example? Animating the padding on one of your widgets is going to screw up the rest of your display.
from tkinter import *
import time
root = Tk()
lbl = Label(root, text='')
lbl.grid(row=0, column=0)
def animation(step=12):
step = 12 if step < 0 else step
lbl['text'] = ' ------ '[step:step+6]
root.after(200, lambda: animation(step-1))
Button(root, text='Animate', command=animation).grid(row=1, column=0, sticky='w')
root.mainloop()

Bind a keystroke to a button in Tkinter

I'm brand new to python (started today). I'm looking to automate something I do a lot of, so far I have 40% of what I need from googling and trying things out that I have found.
I'm trying to produce a counter, when I click the button, I want the counter to increase - I have this bit working...
from tkinter import *
root = Tk()
#Definitions of the fruit, links in with buttons with e1/2/3
def Appleadd_1(event):
value = int(e1.get())
value += 1
e1.delete(0, 'end')
e1.insert(0, value)
def Pearadd_1():
value = int(e2.get())
value += 1
e2.delete(0, 'end')
e2.insert(0, value)
def Grapeadd_1():
value = int(e3.get())
value += 1
e3.delete(0, 'end')
e3.insert(0, value)
#text boxes for counts
e1 = tk.Entry(root)
e1.insert(0, 0)
e1.pack()
e2 = tk.Entry(root)
e2.insert(0, 0)
e2.pack()
e3 = tk.Entry(root)
e3.insert(0, 0)
e3.pack()
#buttons
bt1 = tk.Button(root, text="Apples", command=Appleadd_1)
bt1.bind("<q>" , )
bt1.pack()
bt2 = tk.Button(root, text="Pears", command=Pearadd_1)
bt2.pack()
bt2.bind("1", bt2)
bt3 = tk.Button(root, text="Grapes", command=Grapeadd_1)
bt3.pack()
root.mainloop()
Although it looks ugly, it works and I have just found how to place things instead of using pack()
One thing I can't get to work, is binding a keyboard key to the buttons I have created. Is it possible?
Any help would be greatly appreciated!
Thank you
First off, you'll gain a lot by rewriting your code to use OOP. Tkinter like many other toolkits work best when using inheritance and classes to group widgets together.
As for your actual question, yes you can bind functions to keystrokes in tkinter, and it's relatively easy.
import tkinter as tk
def on_button_click(self, event=None): # command= takes a function with no arguments while .bind takes a function with one argument
print("Clicked the button!")
root = tk.Tk()
button = tk.Button(root, text="Click me!", command=on_button_click)
root.bind("<Control-a>", on_button_click)
Note that you can bind more than keypresses to functions. You can also bind mouse events like scrolling, clicking, or dragging the mouse, various keybind combinations like Shift+Tab, or Ctrl+F, and other events like the <Configure> event which is triggered when the window changes size, or the <Enter> and <Leave> event which are fired when you hover over the bound widget.
You must be wary though, because by default, a new binding will replace the existing binding (unless you pass in '+' as the bind method's third argument), and will trigger the callback for the widget that is currently focused (when applicable). For general purpose bindings, you should bind them to the root widget if possible.

Fast changing labels in Tkinter?

I would like to display some numbers, as fast as possible in Tkinter. The Program, I am trying to do, gets many numbers send and should show those.
Here is an similar environment, where tinter has to change a label very quickly.
from tkinter import *
import time
window = Tk()
lbl13 = Label(window, text="-")
lbl13.grid(column=0, row=0)
x = 0
while 1:
lbl13.config(text = str(x))
time.sleep(2)
x +=1
window.mainloop()
The Tkinter window doesn't even open on my computer. Is that because i have too weak hardware? What could I change that this Program also runs on my Computer. Thank you for every answer!
The infinite while loop will keep the program from getting to the line where you call window.mainloop(). You should call window.update() repeatedly instead of window.mainloop() at the end:
from tkinter import *
import time
window = Tk()
lbl13 = Label(window, text="-")
lbl13.grid(column=0, row=0)
x = 0
while 1:
lbl13.config(text = str(x))
window.update()
x +=1
Using after and a proper mainloop is probably a more more flexible way to achieve what you want; it is also reusable in different contexts, and can be used in an application that does more than trivially increment a number on a label:
maybe something like this:
import tkinter as tk
if __name__ == '__main__':
def increment():
var.set(var.get() + 1)
label.after(1, increment)
window = tk.Tk()
var = tk.IntVar(0)
label = tk.Label(window, textvariable=var)
label.pack()
increment()
window.mainloop()

Problem with calling functions with tkinter

from tkinter import *
from random import *
root = Tk()
#A function to create the turn for the current player. The current player isnt in this code as it is not important
def turn():
window = Tk()
dice = Button(window, text="Roll the dice!", bg= "white", command=lambda:diceAction(window))
dice.pack()
window.mainloop()
#a function to simulate a dice. It kills the function turn.
def diceAction(window):
result = Tk()
y = randint(1, 6)
quitButton = Button(result, text="Ok!", bg="white", command=result.destroy)
quitButton.pack()
window.destroy()
result.mainloop()
#A function to create the playing field and to start the game
def main():
label1 = Button(root, text="hi", bg="black")
label1.pack()
while 1:
turn()
print("Hi")
turn()
main()
root.mainloop()
My problem is that the code in the while function after the first turn() the code isnt executed until i close the root window(which i dont want because it represents the playing field). You can copy this code and execute it yourself if you want.
I have no idea what causes this and havent found anything online. Sorry for the long code but i wrote it so that it is executeable.
I don't know why this particular problem is occurring, but there are a couple of things in your code that are considered bad practice.
Instead of creating multiple instances of Tk(), you should use Toplevel widgets for any pop-up windows needed. Also, it's better to use root.mainloop() to run the program rather than a while loop.
I've made some edits to your code so that it uses a Toplevel widget and discards of the while loop.
from tkinter import *
from random import *
#A function to create the turn for the current player. The current player isnt in this code as it is not important
def turn(prev=None):
# destroy the previous turn
if prev:
prev.destroy()
# pop up with dice
window = Toplevel()
dice = Button(window, text="Roll the dice!", bg= "white")
dice.config(command=lambda b=dice, w=window:diceAction(b, w))
dice.pack()
#a function to simulate a dice, reconfigures the pop-up
def diceAction(button, window):
# roll dice
y = randint(1, 6)
# use dice result here?
print(y)
# reconfigure button, the command now starts a new turn
button.config(text='ok', command=lambda w=window:turn(prev=w))
root = Tk()
# I hijacked this button to use as a start button
label1 = Button(root, text="hi", bg="black", command=turn)
label1.pack()
root.mainloop()
I don't know if this is what you need, but it functions as the code in the question would if it worked.
Sorry I couldn't help with the cause of the error.

Categories