I am trying to remove the title bar of a tkinter window. I want to make a custom title bar. I have searched for this answer and I found this.
import tkinter as tk
root = tk.Tk()
# eliminate the titlebar
root.overrideredirect(1)
# your code here ...
root.mainloop()
When I run this code, the code runs without an error, but no window shows. If I replace
root.overrideredirect(1)
with
root.overrideredirect(0)
then it will show a normal mac style window with the three buttons in the corner.
Edit: I have also tried this
import tkinter as tk
root = tk.Tk()
# eliminate the titlebar
root.wm_attributes('-type', 'splash')
# your code here ...
root.mainloop()
This is the error message that I get
Traceback (most recent call last):
File "no-bar.py", line 5, in <module>
root.wm_attributes('-type', 'splash')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1967, in wm_attributes
return self.tk.call(args)
_tkinter.TclError: bad attribute "-type": must be -alpha, -fullscreen, -modified, -notify, -titlepath, -topmost, or -transparent
What can I do to create a tkinter window without a title bar?
Python 3.8.1
MacOS 10.15.6
EDIT: This only applies to tk/tcl versions < 6.8.10 on macOS
After searching a bit, I found the answer for mac users.
If you only use
root.overrideredirect(1)
Then the window will be hidden on a mac. So you need to add one more line of code so it looks like this
root.overrideredirect(1)
root.overrideredirect(0)
This will show a blank window.
tkinter blank window on mac
Try this:
root.wm_attributes('-type', 'splash')
instead of
root.overrideredirect(1)
I am not sure if this works. Also, I can't test it as I am not a Mac user.
I think it also works on tk/tcl 8.6.8
But you need to write like this:
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
If you add anything between line1 and line2,3, like below, it won't work correctly.
DONT DO THIS!
root = tk.Tk()
root.configure(background='#292929')
root.attributes('-alpha', 0.9)
root.title('Monitor v{0}'.format(VER))
size = '%dx%d+%d+%d' % (340, 200, 0, 0)
root.geometry(size)
root.resizable(width=False, height=False)
root.protocol('WM_DELETE_WINDOW', lambda: sys.exit(0))
root.overrideredirect(True)
root.overrideredirect(False)
Related
i was having some problem in tkinter GUI in python. i write the code to open a blank window :
from tkinter import *
window = Tk()
window.mainloop()
in this moment , there is no error and opens a blank window. but when i want to add label or button or window size , like this code :
from tkinter import *
window = Tk()
window.mainloop()
window.minsize(400,300)
window.maxsize(640,450)
window.geometry('350x200')
now in this code , i wrote a size change code . but it gives an error now and the size code doesnt work. it gives the following error :
Traceback (most recent call last):
File "C:\Users\Green\Desktop\coding\main.py", line 5, in <module>
window.minsize(400,300)
File "C:\Users\Green\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2196, in wm_minsize
return self._getints(self.tk.call(
_tkinter.TclError: can't invoke "wm" command: application has been destroyed
can you help me in this code ? thanks.
Widgets need to go in between window = Tk() and window.mainloop. So, try:
from tkinter import *
window = Tk()
window.minsize(400,300)
window.maxsize(640,450)
window.geometry('350x200')
# Code to add widgets will go here...
# For example:
button = Button(window, text="example button")
button.pack()
window.mainloop()
I am trying to get a Tkinter popup to display when a button is clicked.My issue is that every thing runs just fine except the popup will not produce. I have tried multiple ways to create the popup using tkMessagebox and Toplevel() but still not luck. The program runs but when the button is click nothing happens. I have referenced similar post but still can not find the issue in my code. Any thoughts?
from tkinter import *
def new():
root2 = Tk()
root2.geometry('250x250')
l = Label(root2,text="Please Scan Tag").pack()
root2.mainloop()
# setting main frame
root = Tk()
root.geometry('800x650')
root.title("Pass")
root.configure(background= "white")
label_0 = Label(root, text="Pass",width=10,font=("bold", 50),fg= "green",bg="white")
label_0.place(x=186,y=76)
Button(root,command="new", text='new',font=
("bold",15),width=15,height=4,bg='blue',fg='white').place(x=155,y=300)
root.mainloop()
The command option requires a reference to a callable function, not a string.
Button(root,command=new, ...)
I have a python tkinter app script which can take screenshots on pressing the button. But unfortunately while taking the screenshot, the window of the app also gets captured.
Here's the code of the app:
from PIL import ImageGrab
import tkinter as tk
from tkinter import filedialog
import time
def takeShot():
sc = ImageGrab.grab()
path = filedialog.asksaveasfilename(defaultextension='.png')
sc.save(path)
root = tk.Tk()
canvas1 = tk.Canvas(root,width=300,height=300)
canvas1.pack()
but = tk.Button(text='Take Screenshot',command=takeShot,bg='green',fg='white',font=10)
canvas1.create_window(150,150,window=but)
root.mainloop()
Though it takes the screenshot but app window also gets captured
I don't want that white window in the screenshot. I tried to minimize the window and then maximise by updating the function like this:
def takeShot():
root.withdraw()
sc = ImageGrab.grab()
root.deiconify()
path = filedialog.asksaveasfilename(defaultextension='.png')
sc.save(path)
but it didn't work.
Is there anyway to minimize the app then take the screenshot and then maximize it for save dialogue box so that fulscreen gets captured ??
You need to call root.update_idletasks() after the call to root.withdraw() to force tkinter to redraw the window (or in this case, to remove the window from view).
If you find that root.update_idletasks() doesn't cause the window to hide on your platform, you can try root.update(). update_idletasks should work, though.
I don't have the issue in my Windows running Python 3.8.1. However, I suggest to move the asksaveasfilename(...) right after root.withdraw() as asksaveasfilename(...) is a modal dialog and returns after it is closed:
def take_snapshot():
root.withdraw()
filename = filedialog.asksaveasfilename(initialdir='.', defaultextension='.png')
if filename:
image = ImageGrab.grab()
image.save(filename)
root.deiconify()
I'm using a tkinter.ttk window and I'm using an icon to set the iconbitmap of my window. However root.iconbitmap() is ignored on Windows 10. But There is an easy way to avoid an error: root.tkinter.call('wm', 'iconphoto', root._w, icon)
So:
from tkinter import *
from tkinter.ttk import *
root=Tk()
root.call('wm', 'iconphoto', root._w, icon)
works.
BUT:
def func():
root=Tk()
root.call('wm', 'iconphoto', root._w, icon)
does NOT work. An error occurs. It's interesting that that error is exactly the same one that occurs when you use root.iconbitmap():
Traceback (most recent call last):
File "E:\test.py", line 95, in <module>
func()
File "E:\test.py", line 36, in func
t.call('wm', 'iconphoto', t._w, icon)
_tkinter.TclError: can't use "pyimagex" as iconphoto: not a photo Image
And there is one interesting fact left: In another file I tried to use it as a function too, it worked. In the new file (test.py) it didn't work (and it was the same function).
Does anybody know why it doesn't work and what I can do to avoid an error? Thanks in advance...
If you've a window opened already and wants to open another one with it's own icon then you should use Toplevel() instead of Tk() and to change the icon use
W2 = Toplevel()
icon = PhotoImage(file='icon.png')
W2.tk.call('wm', 'iconphoto', root._w, icon)
Example:
from tkinter import *
from tkinter.ttk import *
def test():
root = Toplevel()
icon = PhotoImage( file='icon.png' ) # path to the icon
root.tk.call('wm', 'iconphoto', root._w, icon)
r = Tk()
b = Button(r, text='press', command=test)
b.pack()
mainloop()
I'm trying to write simple things in a Tkinter module using python 3.6 in Anaconda. This is my code
from tkinter import *
root = Tk()
thelabel = label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
but I get the error:
TclError: can't invoke "label" command: application has been destroyed
ERROR: execution aborted
and I keep getting a blank Tkinter window whenever I run my code and I don't understand what is the problem, thanks:)
You have a small error...
Here is the correct way:
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
You should try reading a bit on tkinter.
Here are some references specifically on label.
Hope you find this helpful!
change your code to this
from tkinter import *
root = Tk()
thelabel = Label(root, text="this is our tk window")
thelabel.pack()
root.mainloop()
Labels start with a capital L not small l it will fix your error