I was trying to call the file dialog of ubuntu to choose a directory with python3.6, and the code looks like this:
from tkinter import filedialog
filedialog.askdirectory()
but when i run this, a very old version file dialog shows:
Any idea on how to call the newest file dialog of ubuntu using python?
It is not old version, it is standard theme for GTK. You would have to use theme to change it. But Linux has only three styles as default
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
root = tk.Tk()
root.style = ttk.Style()
print(root.style.theme_names())
root.style.theme_use('clam')
filedialog.askdirectory()
root.mainloop()
classis/default:
clam:
alt:
You can get more themes installing module
pip install ttkthemes
And code
import tkinter as tk
from tkinter import ttk
import ttkthemes
root = tk.Tk()
root.style = ttkthemes.ThemedStyle()
for i, name in enumerate(sorted(root.style.theme_names())):
b = ttk.Button(root, text=name, command=lambda name=name:root.style.theme_use(name))
b.pack(fill='x')
root.mainloop()
List of styles
kroc:
radiance:
The UI components provided by tkinter (and the underlying tk library) are different from the UI components provided by, say, the GTK or the Qt libraries that are probably used by your desktop.
tkinter has a set of alternative widgets, that you can access with
from tkinter.ttk import *
that support the look and feel of your desktop, but (afaict) unfortunately the
filedialog widget is not supported.
Related
I want to use a download font in Tkinter for my Python project, I read a few answers on another post but my pyglet module won't work, is there an alternative way to use my font
This worked for me (with Pycharm's pyglet library) and can be used to add downloaded fonts to Tkinter (but once again your platform must support the pyglet module):
import tkinter as tk
import pyglet
# replace 'font.ttf' with your ttf file
pyglet.font.add_file('font.ttf')
root = tk.Tk()
# replace 'font' with your font name
MyLabel = tk.Label(root, text="font test", font=('font', 15))
MyLabel.pack()
root.mainloop()
I am using Python 3.5.0 on Windows 10 and want to replace this:
To change the icon you should use iconbitmap or wm_iconbitmap I'm under the impression that the file you wish to change it to must be an ico file.
import tkinter as tk
root = tk.Tk()
root.iconbitmap("myIcon.ico")
If you haven't an icon.ico file you can use an ImageTk.PhotoImage(ico) and wm_iconphoto.
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
ico = Image.open('test.jpg')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
root.mainloop()
Note:
If default is True, this is applied to all future created toplevels as
well. The data in the images is taken as a snapshot at the time of
invocation.
Detailed implementations under different OS:
On Windows, the images are packed into a Windows icon structure. This
will override an ico specified to wm iconbitmap, and vice versa.
On X, the images are arranged into the _NET_WM_ICON X property, which
most modern window managers support. A wm iconbitmap may exist
simultaneously. It is recommended to use not more than 2 icons,
placing the larger icon first.
On Macintosh, this sets the Dock icon with the specified image.
See more
Supported formats since TkVersion 8.6 of tk.PhotoImage(filepath):
PNG
GIF
PPM/PGM
Therefore code can be simplified with a .png file to:
import tkinter as tk
root = tk.Tk()
photo = tk.PhotoImage(file = 'test.png')
root.wm_iconphoto(False, photo)
root.mainloop()
input for tkinter
from tkinter import *
app = Tk()
app.title('Tk')
app.geometry('')
app.iconbitmap(r'C:\Users\User\PycharmProjects\HelloWorld\my.ico')
app.mainloop()
input for pyinstaller
pyinstaller --onefile -w -F --add-binary "my.ico;." my.py
Here is another solution, wich doesn't force you to use an ico file :
from tkinter import *
root = Tk()
root.geometry("200x200")
root.iconphoto(False, tk.PhotoImage(file='C:\\Users\\Pc\\Desktop\\icon.png'))
root.mainloop()
You must not have favicon.ico in the same directory as your code or namely on your folder. Put in the full Pathname. For examples:
from tkinter import *
root = Tk()
root.iconbitmap(r'c:\Python32\DLLs\py.ico')
root.mainloop()
This will work
If you are using CustomTkinter:
app.wm_iconbitmap('yt.ico')
CustomTkinter documentation
from tkinter import *
root = Tk()
root.title('how to put icon ?')
root.iconbitmap('C:\Users\HP\Desktop\py.ico')
root.mainloop()
Here's another way via Tcl command:
import tkinter as tk
window=tk.Tk()
window.tk.call('wm', 'iconphoto', win._w, tk.PhotoImage(file=r"my_icon.png"))
window.mainloop()
I have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.
I want to use mttkinter and unfortunately I must use Python 2.7.
Suprisingly, I cannot find any information on whether ttk widgets become thread safe when using mttkinter.
Do I just have to issue
from mttkinter import mtTkinter as tk
import ttk
root = tk.Tk()
# --- use tk and ttk as usual ---
or possibly alternatively
import Tkinter as tk
import ttk
import mttkinter
root = tk.Tk()
# --- use tk and ttk as usual --
and everything will work as expected? Is there a preferred version of doing the imports?
The wiki on github states
As the mtTkinter module modifies Tkinter in memory, there is no need to change anything else in your program. Just import it once somewhere in your program, and everything should work smoothly.
However, this says nothing about ttk. Can anybody give me confirmation that using ttk is fine?
why doesn't my python program have default windows 7/8/xp buttons but rather dull windows 2000 buttons? How can I fix this?
My dull buttoned program
What I expect :
Use the ttk module for those effects:
import Tkinter
import ttk
root = Tkinter.Tk()
ttk.Button(text="Hello").grid()
root.mainloop()
Example: