Tkinter Dark Theme by Default with Python 3.10? - python

I've just updated Python to 3.10 and when I run Tkinter programs they appear with a dark theme which I've never seen before. I'd like to go back to the standard light theme but I can't figure out how to. There doesn't seem to be any obvious documentation online regarding this.
Here is some quick sample code that just displays a grid to reproduce the dark theme (I'm using Python 3.10 and Visual Studio Code 1.61.2):
from tkinter import *
root = Tk()
class Something:
def __init__(self, parent, col, row):
canvas = Canvas(parent, bd=1, relief=SOLID, highlightthickness=0, width=30, height=30)
canvas.grid(column=col, row=row)
frame = Frame(root, bd=1, relief=SOLID)
frame.grid(padx=50, pady=50)
for i in range(11):
for j in range(11):
Something(frame, i, j)
root.mainloop()

There might be a problem with your default system UI.
I don't have a MAC so I can't test this, but try changing your default system ui to light, if MAC even has that option...

Related

Python Tkinter color not working, it is the same whether I change the customization with canvas

I apologize in advance for any ignorant mistakes I make, I am still a beginner.
I recently started using tkinter on python, and while I tried using the backround customization in tkinter, It does not change any colors in the backround, the same goes with the frame. below is the code I have written.
import tkinter
import tkinter as tk
class GUI:
root = tkinter.Tk()
canvas = tk.Canvas(
height=700,
width=700,
bg="#263D42"
)
canvas.config(bg="green")
canvas.pack()
root.mainloop()
frame = tk.Frame(root, bg="white")
frame.place(relheight=0.8, relwidth=0.8)
I am using M1 MacOS if that information helps, here is a screenshot of the GUI, it is the same when I comment out the color customization in the code.

Python tkinter button font is getting pixelated in bigger sizes

First of all, thank you for trying to help me out. I am currently programming my first GUI with tkinter and i try to create buttons with big fonts, cause i want to create a program for visually impaired people. Sadly I have two problems i can't solve with the internet on my own ..
This is the relevant part of my code: (sorry for the german variables)
import tkinter
from tkinter import *
from tkinter import font
import tkinter.messagebox
class Oberflaeche(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.pack()
MyFont = font.Font(family='times', size=50)
self.close_window = tkinter.Button(self, font=MyFont, text="Programm \nbeenden", command=self.close_window, bg="white", height = 3, width = 18, bd=3, relief="solid")
self.close_window.pack()
def close_window(self):
root.destroy()
root = tkinter.Tk()
root.title("Prototyp MVP")
root.minsize(width=300, height=300)
root.configure(background='white')
oberflaeche = Oberflaeche(master=root)
oberflaeche.mainloop()
when I try to change the font tkFont.Font is not working. There is the Error:
NameError: name 'tkFont' is not defined
That is the reason I tryed font.Font. But no matter how I change the font family or type, it always looks awful and pixelated....
Picture of the failed Button
I am using python 3.5.5, Ubuntu 16.04 and tk 8.6.8.
I am using Spyder(python 3.6) here is the result;
i.hizliresim.com/6NgZlW.jpg
Maybe because of version?
Actually your program is working succesfully ı did not change any codes ı tried on my pc and the result is perfect(Win10 Spyder(Python 3.6).)
i am also used to Jupyter notebook and i was trying with VS Code everything is working well. I suggest you trying to play around with scaling and let me know is it working for you?
Scaling of Tkinter GUI in 4k (3840*2160) resolution
Results
Jupyter
VS Code

Tkinter window won't update/refresh until I switch to another window (like finder)

Mac OSX, Python 2.7, Tkinter
When my program runs and my root window opens, any input in an entry field is recorded, but not displayed on the screen...until you 1. resize the whole window manually, or 2. switch to another application window, like finder or Outlook, and switching back to your root window.
from Tkinter import *
root = Tk()
root.attributes("-topmost", True)
label1 = Label(root, text="Enter your name: ")
label2 = Label(root, text="What's your favorite color? ")
entry1 = Entry(root)
entry2 = Entry(root)
label1.grid(row=0)
label2.grid(row=1)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
#two options I tried to no avail
#root.update_idletasks()
#root.update()
root.mainloop()
Am I programming something incorrectly? (I took this example from a youtube video that worked)
Is there something incompatible with my computer and these software versions?
Thanks for taking a look. I've searched everywhere and can't find any other references of this happening.
Make sure you are using the right tcl/tk for your OSX version. What do you mean by 'recorded, but not displayed '? If you cannot see a letter you type, what do you mean by 'recorded'? You code looks fine on my Win7 machine.

ttk.ComboBox Styling does not get set properly

I have created a python GUI application. It works great, and I've styled everything to my liking, save for the ComboBox. Styling on the ttk.Combobox doesn't seem to work.
That should give an idea of the material style I'm going for. Here's the styling block I have for the combobox.
globalStyle = ttk.Style()
globalStyle.configure('TCombobox', foreground=textColor, background=backgroundColor, fieldbackground=selectColor, fieldforeground=textColor, font='Verdana')
The only thing I have been able to successfully change is the text and the foreground color. I am looking to edit the following attributes:
Text color
Field background
Dropdown text color
Dropdown background
EDIT: I should mention that the color variables used are all valid hex color codes.
selectColor = '#333333'
backgroundColor = '#444444'
foregroundColor = '#555555'
textColor = '#999999'
So I ran in to the same issue but found most of the solution here. All you have to do is add the following to your code:
option add *TCombobox*Listbox.background color
option add *TCombobox*Listbox.font font
option add *TCombobox*Listbox.foreground color
option add *TCombobox*Listbox.selectBackground color
option add *TCombobox*Listbox.selectForeground color
Then to change the font inside the box (when the drop down isn't present) add font='font_style' to your code.
So in my case I had:
class CreateProfile(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='dodgerblue4')
label = tk.Label(self, text="Create Profile", font=large_font, bg='dodgerblue4', fg='deepskyblue')
label.grid(columnspan=10, row=0, column=0, pady=5, padx=5)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(1, weight=1)
self.option_add("*TCombobox*Listbox*Background", "dodgerblue")
self.option_add("*TCombobox*Listbox*Font", "pirulen")
self.list_c = ttk.Combobox(self, values=("1", "2", "3", "4"), font='pirulen')
self.list_c.grid(row=1, column=1, pady=5, padx=5)
Make sure you also have the following imports:
import tkinter as tk
import tkinter.ttk as ttk
My issue is I'm only able to change the background color of the actual box (when the drop down isn't present). I'm still trying to figure out how to change the font color (foreground doesn't work for me), and the color of the box itself. So if anybody could add to this answer that would be great!
I know that this question is half a year old, but I had a similar issue and managed to solve it. For changing the color of a ttk Combobox popdown frame you can use the following code:
# these imports are indeed only valid for python 3.x
import tkinter as tk
import tkinter.ttk as ttk
# for python 2.x the following import statements should work:
# import Tkinter as tk
# import ttk
root = tk.Tk()
# adding the options to the root elements
# (all comboboxes will receive this options)
root.option_add("*background", "#444444"),
root.option_add("*foreground", "#999999"),
# create a combobox
ttk.Combobox(root, values=["Banana", "Coconut", "Strawberry"]).pack()
root.mainloop()
I'm not sure whether I understand the styling mechanisms of tk correctly.
At least the above code works for me on Python 3.2 and Python 3.4

Tkinter Linux version of "<Control-Shift-u>"

What's the Linux version of "<Control-Shift-u>" for keybindings in Tkinter? You might be tempted to think it's exactly that, but, alas, it does not seem to be. For instance, the Linux version of "<Control-Shift-Tab>" is "<Control-ISO_Left_Tab>". I've searched and haven't found any documentation for this.
The following will do what you want:
from Tkinter import *
def proof(event=None):
print 'ping'
root = Tk()
frame = Frame(root, height=100, width=100)
frame.focus_set()
frame.bind('<Control-Shift-KeyPress-U>', proof)
frame.pack()
root.mainloop()
The u becomes capitalized because of the shift modifier and you want to capture the KeyPress event.

Categories