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:
Related
I am using Tkinter to make GUI. I included a messagebox in my code, here's the detail:
from tkinter import *
from tkinter.messagebox import *
root=Tk()
askyesno(message="Are you sure you want to choose this text file?")
When the askyesno pops up, there are 2 buttons: Yes and No, but I want to customize these buttons to "OK" and "Cancel", for example. I have been looking for how to customize Tkinter messagebox buttons many times but I found nothing. Any help? Thanks
Use messagebox.askokcancel
from tkinter import Tk,Button,messagebox
root=Tk()
def create_message():
messagebox.askokcancel("Title",'Hello')
b=Button(root,text='Message',command=create_message)
b.pack()
root.mainloop()
You can simply use
from tkinter import *
from tkinter import messagebox
root = Tk()
button=Button("Message",)
messagebox.askokcancel()
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?
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.
I ran this code and the RAM in my computer with my processor looks like it's going to explode! What is the reason?
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import os
bloque1=Tk()
bloque1.title('Bloque1')
bloque1.config(bg="#1C1C1C")
bloque1.geometry("450x410")
barramenu=Menu(bloque1)
menubar=Menu(bloque1)
menubar.add_cascade(label="Actividades", menu=menubar)
menubar.add_command(label="Instrucciones")
menubar.add_command(label="Ayuda")
menubar.add_command(label="Cerrar", command=bloque1.quit)
bloque1.config(menu=menubar)
bloque1.mainloop()
You are adding a menu to itself. No doubt this is causing an infinite loop inside of Tkinter.
menubar.add_cascade(label="Actividades", menu=menubar)
That menu= attribute needs to be given another menu that will appear when you select that cascade entry from the menubar.