tkinter messagebox always appears behind main pygame window - python

I am trying to make my messagebox appear in front of my pygame window, but it keeps appearing behind it. Here's my code:
from tkinter import messagebox
# pygame loop here
messagebox.showinfo("Title", "Message here")
Do I need to do add some lines of code to bring it to the front? Any help would be appreciated.

I got it to work. I had to add root.withdraw() as well.
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root.withdraw()
# pygame loop here
messagebox.showinfo("Title", "Message here")
root.lift()
Not sure why hiding the root tkinter window makes it work...

This will put the window in the middle of the screen on the top level of everything, so it will not be hidden behind and stays in front.
window = Tk()
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())

Related

How would I get these two pieces of code to run together?

I am looking for a way to hide / show a tkinter window using the key p.
import keyboard
root = tk.Tk()
root.geometry("1000x1000")
greeting = tk.Label(text="Hello, Tkinter.")
greeting.pack(pady=10)
root.mainloop()
while not keyboard.is_pressed('p'):
root.withdraw()
while not keyboard.is_pressed('p'):
root.deiconify()
My problem is that I can't get the code to run infinitely without messing up the root.mainloop().
I seriously have no idea what to do.
The code I'm talking about is after the mainloop.
You have to bind the key to do something. Heres an example:
import tkinter as tk
root = tk.Tk()
def key_presses(e):
print('q was pressed')
root.bind('q', key_pressed)
The code above prints 'q was pressed', well every time it's pressed.

new tkinter window loses focus after mainloop ends

I'm using the following code to create a window after destroying another.
from tkinter import *
tk=Tk()
def destroy():
tk.destroy()
tk.after(2000,destroy)
tk.mainloop()
tk=Tk()
tk.mainloop()
The window is created alright, but it loses focus. I tried lift() and focus() methods with no result.
You can use <tk.Tk>.focus_force() to force the window to be focused.
So your modified code will look like this:
from tkinter import *
tk = Tk()
def destroy():
tk.destroy()
tk.after(2000, destroy)
tk.mainloop()
tk = Tk()
tk.focus_force()
tk.mainloop()
Although it is much better to reuse the window. Destroy all of the widgets on the window and reuse it instead of destroying and recreating it. Creating a window takes a lot of resources.
Also something else: the variable tk is usually used for something else so please don't use it for tkinter windows. Usually people use root or window for windows.
Force the input focus to the widget using focus_force(). Just update you last part.
tk=Tk()
tk.focus_force()
tk.mainloop()
Also, You can make a Toplevel() which is basically just creating a new window over the root window.
from tkinter import *
tkk=Tk()
tkk.withdraw()
tk=Toplevel()
def destroy():
tk.destroy()
tkk.deiconify()
tk.after(2000,destroy)
tk.focus_force()
tkk.mainloop()

Removing Base Window When Using Tkinter Python 3 (Askopenfiledialogue)

This is my first post on here, so bear with me as far as post etiquette goes. I've been struggling to get rid of the base Tkinter window that appears when I'm using the askopenfilename function of Tkinter. I've tried using Withdraw and destroy (in combination and alone) to fix this issue, but it seems to leave my code stuck in a loop and unable to continue to the next sections.
I have seen several solutions to this in Python 2, but I have no idea how they translate to python 3. Tkinter isn't a module I have a lot of experience with, so it is likely a simple oversight I am making.
Any suggestions or comments are much appreciated
Here is a sample of my code I am using (CSV module is for another section of my code)
import csv
from tkinter import filedialog
from tkinter import *
root= Tk()
root.filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
root.mainloop()
Use withdraw to hide root window, deiconify to show root window
import csv
from tkinter import filedialog
from tkinter import *
root=Tk()
# Hide Window
root.withdraw()
filename= filedialog.askopenfilename(initialdir = r"\Users",title="Select A File", filetypes= [("Csv Files","*.csv")])
# Show Window
root.deiconify()
root.mainloop()

Python 3 tkinter: focus_force on messagebox

I'm running python 3 code in background which should show a popup window in some situations. I'm using tkinter for this:
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
root.wm_attributes("-topmost", 1)
messagebox.showinfo(title, message, parent=root)
root.destroy()
popup('foo')
The ok-button in this infobox should get the focus automatically when popping up. Sadly I'm not able to do this. I tried root.focus(), but it does not help. Any ideas how to solve that? TIA
BTW: The code should be platform independent (Linux and Windows).
Edit:
Maybe I missunderstood the focus keyword and I should clarify my question:
root = tk.Tk()
root.focus_force()
root.wait_window()
When calling the code above the root window is active, even if I worked in e.g. the browser before. Is this also possible for messagebox.showinfo? Adding root.focus_force() in the popup function does not help.
Is this even possible? Or is it necessary to create my own root window? I really like the appearance of the messagebox with the icon.
Edit 2:
Here is a video: https://filebin.net/no195o9rjy3qq5c4/focus.mp4
The editor is the active window, even after the popup was shown.
In Linux I it works as expected.
You can use the default argument in the messagebox function.
default constant
Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL, YES, or NO (the constants are defined in the tkMessageBox module).
So, here is an example to highlight the "ok" button.
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
messagebox.showinfo(title, message, parent=root, default = "ok")
root.destroy()
popup('foo')
Hope this helps!

How to move the entire window to a place on the screen (Tkinter, Python3)

The title says it all. How to move the entire window to a place on the screen using tkinter. This should be moving the root frame.
Use the geometry method of the root (or any Toplevel) window. For example:
import tkinter as tk
root = tk.Tk()
root.geometry("+200+400") # places the window at 200,400 on the screen
use this:
from tkinter import Tk
main=Tk()
main.geometry('+100+200')
main.mainloop()
or do it with function :
def change_position(root_variable,x,y):
root_variable.geometry('+{}+{}'.format(x,y))
and use :change_position(main,500,400)
edit: added dot for format

Categories

Resources