Prevent tkinter window from minimizing but able to maximise and close window - python

I want to prevent my tkinter window from minimizing but able to maximise and close the window.
Something like this
How do I do this?

If your platform is Windows (looks like it is based on the image), you can call Windows functions via pywin32 module to achieve the goal:
import tkinter as tk
import win32gui
import win32con
root = tk.Tk()
root.title('MinimizeTest')
root.geometry('300x200')
def disable_minbox():
# get window handle
win_id = root.winfo_id()
hwnd = win32gui.GetParent(win_id)
# get the current window style of root window
style = win32gui.GetWindowLong(hwnd, win32con.GWL_STYLE)
# mask out minimize button
style &= ~win32con.WS_MINIMIZEBOX
# update the window style of root window
win32gui.SetWindowLong(hwnd, win32con.GWL_STYLE, style)
# need to do it when root window is displayed
root.after(1, disable_minbox)
root.mainloop()
Note that you need to install pywin32 using pip:
pip install pywin32

Related

How to disable Windows key using tkinter?

I am coding a sign in and I would like there to be no way out unless signin is complete. But in order to do that I need to disable the Windows key so they cannot leave the window. I is has no header, I disabled the WM delete window protocol, I have automatic full screen. I also need to make it dynamically set itself to the middle but I am not that far. Messagebox import works.
My Code:
from tkinter import *
import messagebox
from win32api import GetSystemMetrics
def getfullscreensize():
global width
width = GetSystemMetrics(0)
global height
height = GetSystemMetrics(1)
def donothing():
pass
root = Tk()
root.attributes('-fullscreen', True)
root.protocol("WM_DELETE_WINDOW", donothing)
root.overrideredirect(1)
root.bind("<key>", lambda e: "break")
root.mainloop()
How to disable Windows key using tkinter?
In short, you can't. Tkinter does not have any features that allow it to disable OS-level features like the windows key.
In order to disable this key you'll have to find some other platform-specific solution.

Make root window transparent and also other things accessible

I want to set my root window transparent, I have done this easily, but I can't move anything in the background of my system, for example my terminal, or anything else.
from tkinter import Tk
self.tk = Tk()
self.tk.attributes('-zoomed', True) # This just maximizes the window
self.tk.wait_visibility() # just to fix self.tk.attributes
self.tk.attributes('-type', 'dock') # disable title and title buttons
self.tk.attributes('-alpha', 0.1) # transparent
self.tk.mainloop() # main loop
The reason I want to do that, is because I want to make an screenshot application, and I want to make some effects that make the system transparent
You could make any color completly transparent in your window.
from tkinter import Tk
root = Tk()
root.configure(bg='white')
root.attributes('-transparentcolor', 'white')
root.attributes('-topmost', True)
root.mainloop()
I think code speaks for itself. (OS dependent, works on windows)

Tkinter window hangs after restoring from system tray (using pystray)

I'm creating a Tkinter-based GUI in Python. I would like the window to hide to system tray when it is minimized (using pystray module). It hides, but it only appears on the screen and hangs, when I'm trying to restore it.
Here's what I have tried:
from tkinter import *
from PIL import Image
import pystray
def hide_to_tray(_event=None):
tray_icon = pystray.Icon("MyTrayIcon", title="My tray icon") # create the tray icon
tray_icon.icon = Image.open("app_icon.ico") # open the icon using PIL
tray_icon.menu = pystray.Menu(pystray.MenuItem("Open", lambda: tray_icon.stop(), default=True)) # create the menu
root.withdraw() # hide the window
tray_icon.run() # run the icon's main loop
# icon mainloop
root.deiconify() # when the icon mainloop had been stopped, show the window again
root.focus_force() # focus on it
root = Tk()
btn = Button(root, text="Sample button")
btn.grid()
root.bind("<Unmap>", hide_to_tray) # hide to tray on minimizing
root.mainloop()
How can I solve this problem?
1. Use infi.systray
It runs on a seperate thread and thus won't block, install it with pip:
pip install infi.systray
Don't call tkinter methods from the info.systray thread:
Since infi.systray runs on a seperate thread, you mustn't call tkinter methods directly in the callback functions that you pass to the systray icon on creation. Use a thread safe way (e.g. queue) to inform the main thread about events in the systray icon instead!
2. Don't run pystray and tkinter at the same time
You can't run them both because they block the running thread and both have to run on the mainthread.
See Osher's answer that only displays the system tray icon when the tkinter app is closed.

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

Using TCL extensions to set native window style in Tkinter

pythonware.com/library/tkinter/introduction/…
documents a overrideredirect method
that will remove thetitlebar and
borders, if that is not enough you
must set the native window style, I'm
not sure if Tkinter gives you that
kind of low-level access, if not, try
the something like
twapi.magicsplat.com/ui.html#set_window_style
TCL extension
In an earlier post I got this as a reply on how to get a border in Tkinter similar to the one pictured below. I am not familiar with Tcl and it's extensions. So how would go about doing this? The end goal is basicaly to get the border below on a Tkinter window.
Edit :
I used the following on Windows 7 and it didn't seem to change the style. It's probably missing something. Any help would be appreciated, this could be really cool!
import string, win32ui, win32con
import Tkinter as tk
root = tk.Tk()
frame = win32ui.CreateWindowFromHandle(string.atoi(root.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
root.mainloop()
You can do this using a combination of the Python win32 api packages and Tkinter. What you need to know is that a Tk window is the client section of a Win32 window. The window manager interactions are handled using a wrapper that is the parent of Tk window itself. If you have a Tkinter window 'w' then you can create a PyWin32 window for the frame or just manipulate it directly. You can get the frame hwnd using w.wm_frame() and parsing the hex string returned or by using GetParent on the winfo_id value from the Tk window (although wm_frame is likely to be more reliable).
import string, win32ui, win32con
from Tkinter import *
w = Tk()
frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
This removes the WS_CAPTION style and notifies the window that its frame is modified which forces a geometry recalculation so that the change propagates to the Tk child window.
EDIT ---
The following arranges to ensure we modify the window style after the window has been fully created and mapped to the display.
import string, win32ui, win32con
from Tkinter import *
def decaption(event):
w = event.widget
frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
w.bind("<Map>", None)
root = Tk()
root.bind("<Map>", decaption)
root.mainloop()
One solution is to draw your own border. Use overrideredirect to remove all decorations, grid/pack/place a canvas that fills the window, then draw or use bitmaps to get the visual effect you want. You'll have to add your own bindings for moving and resizing tne window, but that's not too difficult.

Categories

Resources