I write an app in python, using Tkinter. I want it to be zoomed and not resizable, but when i do this, it hides the taskbar, which I want to keep. Is there any way with that can be done?
from tkinter import *
root=Tk()
root.state('zoomed')
root.resizable(False, False)
Python 3.8
Windows 10
Related
I want to make a borderless widget with tkinter that lets the user decide if they want the application to show on top of other programs or not.
I'm aware that in order to remove the borders and the toolbar I can use the method root.overrideredirect(True), but this also makes the window topmost automatically, as it makes the window manager ignore the widget.
Because of this, the root.attributes("-topmost",False) and root.wm_attributes("-topmost", False) command don't work.
Is there a way to achieve what I need? For example, another method to hide the window manager decorations or some other way to hide/restore the widget when it loses the focus?
By the way, I'm using Linux Mint 20.2 Cinnamon v5.0.5 with Python 3.8.10.
from tkinter import *
root = Tk()
root.overrideredirect(True)
root.resizable(False, False)
root.geometry("420x100")
root.attributes("-topmost", False)
#root.wm_attributes("-topmost", False)
exitbtn = Button(root, text='Exit',command=root.destroy)
exitbtn.pack(side=BOTTOM)
root.mainloop()
EDIT:
After some search, I've found that root.wm_attributes('-type','splash') works exactly as I want. But it seems that this will only work on Linux. How could I achieve that on Windows?
I had the same question and this worked for me:
Tkinter, Windows: How to view window in windows task bar which has no title bar?
PS: I'm new here so I cannot comment.
Edit: It's for Windows I think. I haven't tried on Linux yet.
I created a .py script that autofill some forms using Selenium. In the start of the code, I used Tkinter extension to use the messagebox and filedialog function.
My code runs fine, but everytime I got an annoying window called "tk" (I guess it is related to Tkinter). This window is blank, but I would like to remove it from my code run.
Is there a way to do this?
Try this:
import tkinter as tk
from tkinter import filedialog
# Create a dummy window
root = tk.Tk()
# Hide the window
root.withdraw()
# Use `filedialog` freely
print(filedialog.askopenfilename())
# If you want to destroy the window at the end.
# You don't have to
root.destroy()
The reason why that window appears is linked to how tkinter handles new windows. It uses tkinter.Toplevel instead of a tkinter.Tk. But a tkinter.Toplevel can't exist without a tkinter.Tk so it creates one. That is the window that you see.
To hide the window you have to first create your own tkinter.Tk and hide it using <tkinter.Tk>.withdraw().
I have made a GUI using Tkinter for my Python script for a Voice Assistant. It is working pretty well. But I want to add an animation window to display an animation that I have created using After Effects as an intro to the app. I want it to open without the default close(x), maximize and minimize buttons. The window should stay till the animation is completed, and then it would disappear. The main window would then open normally to launch the GUI. To disable the close, maximize and minimize buttons I have used the root.overrideredirect(True) method. But I am not being able to simultaneously open the two windows one after the other as mentioned. I would be highly obliged if somebody would help me out with this! I have tested it with a code on a simple GUI. I am providing the code below for help with the problem!
from tkinter import *
import time
root = Tk()
root.geometry('500x300')
root.overrideredirect(True) # To disable the default Window decoration
time.sleep(5) # Assuming that the animation runs for 5 seconds
root.destroy() # This window destroys after being on screen for 5 seconds
root.mainloop()
root2 = Tk() # After the previous window is destroyed, this window opens up. Assume that this is the main window
root2.geometry('500x300')
root.mainloop()
Please help me out!
The thing you call "animation window" is actually called "splash". There is a way to do what you want. You need to create a root window for the app (a Tk instance), and then you should hide (root.withdraw()) it. Now create a Toplevel for the splash, wait for 5 seconds, destroy it, and show (root.deiconify()) the Tk window again.
Note: time.sleep(5) should never be used with Tkinter, use root.after(ms, func) instead.
from tkinter import *
def show_splash():
splash = Toplevel()
splash.geometry('500x300')
splash.overrideredirect(True) # To disable the default Window decoration
splash.after(5000, splash.destroy) # This window destroys after being on screen for 5 seconds
splash.wait_window()
root = Tk()
root.withdraw()
show_splash()
root.deiconify()
root.mainloop()
PS: Procedural programming is not a good idea for pretty complex Tkinter-based apps. You should consider using OOP instead.
PPS: Take a look at this and this answers.
This is my first post, so please give feedback to improve if need be.
I am following the tkinter tutorial on tkdocs to learn tkinter. The current lesson is attempting to teach me how to create menus, but whenever I run their script, a separate window pops up instead of menu attached the the root window.
from tkinter import *
from tkinter import ttk
root = Tk()
root.option_add('*tearOff', FALSE)
win = Toplevel(root)
menubar = Menu(win)
win['menu'] = menubar
root.mainloop()
This pops up as a second window. I'm not sure if I am simply not understanding the material, or if I am doing something wrong. The lessons are written in the mindset that you are using at least python 3 and tkinter 8.6. I have the current Anaconda distribution and it meets both of these requirements.
What am I doing incorrectly to have this code open a separate window as opposed to a menu attached to a window?
I was able to figure out the issue. I misread the tutorial and expected a single window to open up with a menubar and a couple options within that menubar. After messing with the code, I was able to write a script that created the window I originally thought we were going to open. Ultimately, I didn't realize that "TopLevel" would open a separate window, so that was on me. Below is the code that I used to open up a window with the menubar.
from tkinter import *
from tkinter import ttk
root = Tk()
root.option_add('*tearOff', FALSE)
menubar = Menu(root)
menu_file = Menu(menubar)
menu_edit = Menu(menubar)
menubar.add_cascade(menu=menu_file, label='File')
menubar.add_cascade(menu=menu_edit, label='Edit')
root['menu'] = menubar
root.mainloop()
Thanks, and I hope this helps someone else understand what is happening if they are learning tkinter as well.
I made a GUI with Tkinter in python 3. Is it possible to close the window and have the application stays in the Windows System Tray?
Is there any lib or command within Tkinter for this.
The entire solution consists of two parts:
hide/restore tkinter window
create/delete systray object
Tkinter has no functionality to work with the system tray.
(root.iconify() minimizes to the taskbar, not to the tray)
step 1) (more info) can be done by
window = tk.Tk()
window.withdraw() # hide
window.deiconify() # show
step 2) can be done by site-packages, e.g. pystray
(an example, the same example, and more info)
You can use the wm_protocol specifically WM_DELETE_WINDOW protocol. It allows you to register a callback which will be called when the window is destroyed. This is an simple example:
import tkinter as tk
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", root.iconify)
root.mainloop()
.iconify turns the window into an icon in System Tray.