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()
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'm getting this issue as mentioned in the title.
I'm trying to run a file to print hello world in the widget. I'm getting what I want when I run it in my system, but when I'm running it in colab its not working.
Code:
import tkinter
root = tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
Output:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-db74150bd164> in <module>()
1 import tkinter
2
----> 3 root = tk()
4
5 myLabel = Label(root, text="Hello World!")
TypeError: 'module' object is not callable
I tried changing tk into various forms(Tk, tK, Tkinter, tKinter), but it isn't working anyhow.
When you see Tk(), it is an instance of Tk() class present in __init__.py file in tkinter folder.
Since you have imported tkinter, you have to specify tkinter.Tk()to create a instance of Tk()
import tkinter
root = tkinter.tk()
myLabel = Label(root, text="Hello World!")
myLabel.pack()
root.mainloop()
In some programs, you can also see tk.Tk(). This is because the module tkinter is imported as tk:
import tkinter as tk
See the source code of tkinter
At line 2273 in __init__.py, you can see:
class Tk(Misc, Wm):
"""Toplevel widget of Tk which represents mostly the main window
of an application. It has an associated Tcl interpreter."""
_w = '.'
def __init__(self, screenName=None, baseName=None, className='Tk',
useTk=True, sync=False, use=None):
...
There are some errors:
import tkinter as tk
root = tk.Tk() # tk() you can't call a module, write tk.Tk() instead.
myLabel = tk.Label(root, text="Hello World!") # add tk.
myLabel.pack()
root.mainloop()
you can just import tkinter as
from tkinter import *
by using this it will work
I can't see the .icns file
python, tkinter
code:
windows = iconbitmap("any.icns")
enter image description here
The bitmap must be an ico type, but not png or jpg type, otherwise, the image will not display as the icon.
Theese are solutions
import tkinter as tk
root = tk.Tk()
root.tk.call('wm', 'iconphoto', root._w, tk.PhotoImage(file='/path/to/ico/icon.png')
root.mainloop()
import tkinter as tk
root = tk.Tk()
root.iconphoto(False, tk.PhotoImage(file='/path/to/ico/icon.png'))
root.mainloop()
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)
When I run this script, two windows appear, one for the file selection and the Tkinter window. How can I change this so that the Tkinter window only opens after a file has been selected? Thanks
def main():
my_file = askopenfilename()
stage1()
def stage1():
master = Tk()
master.mainloop()
The window master does open only after the file dialog closure (try to change its title to check), the first window you see is the parent window of the file dialog. Indeed, the tkinter file dialogs are toplevel windows, so they cannot exist without a parent window. So the first window you see is the parent window of the file dialog.
The parent window can however be hidden using the withdraw method and then restored with deiconify:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
def main():
master = Tk()
master.withdraw() # hide window
my_file = askopenfilename(parent=master)
master.deiconify() # show window
master.mainloop()
if __name__ == '__main__':
main()