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.
Related
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
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.
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)
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
I am building an Interface with TKinter and I have the problem, that whenever create a new window the standardized tkinter window of 200x200 pixel with nothing in it flashes up for a fraction of a second and AFTER that all my modifications (widgets ect.) are made. This happens before AND after calling the mainloop.
The Main-Interface is created.
Mainloop stats
Flashing window
Main-Interface appears
Also after Mainloop was called this happens with newly created windows.
Main-Interface appears--> Push a button, that creates a new window
Flashing window
new window appears
Sadly I cannot give you a sample code... If I try to do a minimal example, this doesn't happen. Maybe the standard window is created, but it is changed so fast, that it doesn't appear on screen. I don't even know what to look up in this case... searching "tkinter flashing window" yields nothing.
EDIT: I found the source of the problem. It seems to be caused by wm_iconbitmap, FigureCanvasTkAgg and tkinter.Toplevel. If you remove the the icon out of the code, it works fine, no flashing. But if I use it together with one of the other, the window flashes when created. Try it out with the code below. You have to put the icon in the working directory of course.
Here is a code sample and the link to the icon I am using, but I suppose any icon will do.
# coding=utf-8
import numpy as np
import matplotlib as mpl
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import os
class INTERFACE(object):
def __init__(self):
self.root = tk.Tk()
self.root.protocol("WM_DELETE_WINDOW", self.EXIT)
self.root.wm_iconbitmap( os.path.abspath("icon.ico")) #<---- !!!!!!
self.root.geometry("1024x768")
canvas = FigureCanvasTkAgg(self.testfigure(), master=self.root) #<---- !!!!!!
canvas.get_tk_widget().grid(sticky=tk.N+tk.W+tk.E+tk.S)
self.root.rowconfigure(0, weight=1)
self.root.columnconfigure(0, weight=1)
def testfigure(self):
x=np.linspace(0, 2*np.pi,100)
y=np.sin(x)
fig = mpl.figure.Figure()
sub = fig.add_subplot(111)
sub.plot(x,y)
return fig
def EXIT(self):
Top = tk.Toplevel(master=self.root)
Top.wm_iconbitmap( os.path.abspath("icon.ico")) #<---- !!!!!!
Top.transient(self.root)
Top.resizable(width=False, height=False)
Top.title("Exit")
tk.Message(Top,text="Do you really want to quit?", justify=tk.CENTER, width=300).grid(row=0,columnspan=3)
tk.Button(Top,text="YES",command=self.root.destroy).grid(row=1,column=0)
tk.Button(Top,text="No",command=self.root.destroy).grid(row=1,column=1)
tk.Button(Top,text="Maybe",command=self.root.destroy).grid(row=1,column=2)
def start(self):
self.root.mainloop()
if __name__ == '__main__':
INTERFACE().start()
I know this is an old question, but I've experienced a similar situation and have found a solution.
In my case, I've isolated the issue to the use of iconbitmap. I've managed to solve it by calling iconbitmap with the after method just before calling root.mainloop().
Example:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.geometry('300x300+500+500')
root.after(50, root.iconbitmap('icon.ico'))
root.mainloop()
This method has worked on Toplevel() windows with icons as well.
Tested on Win 8.1 with Python 3.5.0.
Edit: Upon further inspection I've noticed that the behavior changes relative to root.geometry's presence as well. My initial example didn't have it and I only noticed after a few tries that it still had the same issue. The time delay in the after method doesn't seem to change anything.
Moving root.geometry below the after method yields the same issue for some reason.
Most likely, somewhere in your initialization code you're calling update or update_idletasks, which causes the current state of the GUI to be drawn on the screen.
Another possible source of the problem is if you're creating multiple instances of Tk rather than Toplevel.
Without seeing your code, though, all we can do is guess.
Your best route to solving this problem is to create a small example that has the same behavior. Not because we need it to help you, but because the effort you put into recreating the bug will likely teach you what is causing the bug.
This should work but it requires win32gui
import win32gui
def FlashMyWindow(title):
ID = win32gui.FindWindow(None, title)
win32gui.FlashWindow(ID,True)