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?
Solution 1:
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