Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can my Python program avoid minimizing itself when the user clicks on its button?

I have a made a very simple program that opens a new tab in the user’s default browser, here:

import tkinter as tk 
import webbrowser


root = tk.Tk()
root.geometry('500x400')
root.title("Bulkdozer")

def open_chrome_tab():
    webbrowser.open_new_tab('https://google.com')

#####BUTTON ZONE#######
open_browser = tk.Button(root, width=20,  text="Open New Tab", command=open_chrome_tab)
open_browser.grid(row=22, column=1)
#####BUTTON ZONE END#######
root.mainloop()

However, when the user clicks on the Open New Tab button, the GUI of this program minimizes itself immediately while opening the corresponding new tab in the user’s default browser.

How can I set a rule that make this program not minimize itself when the user clicks on its button?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easiest way around is to make your window topmost always:

root.attributes('-topmost',True)

Or you can use this hack to temporarily give focus and then remove focus once the browser is up.

def open_chrome_tab():
    webbrowser.open_new_tab('https://google.com')
    root.attributes('-topmost',1)
    root.after(1000,lambda: root.attributes('-topmost',0)) # 1000 ms might be delayed depending on time to load browser
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading