When I create a button like this in tkinter:
self.submition_button=Button(self.root, text='Submit',
font='Times 12 bold italic', command=self.onSubmition, bg='blue')
the button isn't blue.
Why isn't it blue, and what can I do to make it blue?
Are you using tkinter.Button or are you really using ttk.Button?
You'll know which one your using based on what you imported in the top of code
regular tkinter widgets importing
import tkinter
typical TTK widgets importing
from tkinter import ttk
Because TTK is more modern version of the library it's styling system is different. So setting flag options on it wont work. instead you have to use style theme.
See this reply.
https://stackoverflow.com/a/44416355/8661716
This is the code for a blue button, check it :
from tkinter import *
#create a window
root =Tk()
button=Button(root, text='Submit', font='Times 12 bold italic', bg='blue', activebackground="blue")
button.pack()
root.mainloop()
Related
I want to make my own style in a python / tkinter application using ttk Notebooks. I like the style of the aqua setting shown below.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
s = ttk.Style(root)
s.theme_use('aqua')
notebook = ttk.Notebook(root)
frame1 = ttk.Frame(root, width=400, height=400)
frame2 = ttk.Frame(root)
notebook.add(frame1, text="Frame1")
notebook.add(frame2, text="Frame2")
notebook.pack()
root.geometry("400x400")
root.mainloop()
But aqua will not let me change the background color of some widgets so I want to use the default style. Is there a way to configure the TNotebook.Tab using the default style to get the tabs in the middle? Something like
s.configure('TNotebook.Tab', tabposition='center')
but I have not found anything that works.
I couldn't use theme aqua on Windows, but use default theme and
s.configure("TNotebook", tabposition='n')
could make the tab in the center.
I want the label text to be blue, as it is by default on Windows.
I'm following a tutorial on Python and Tkinter that was written for Windows, but I'm doing it on Linux (Python 3.5.1 on xubuntu 16.04.5). Right now I'm puzzled by the fact that all the LabelFrame examples in the book have a blue header label, but when I run their code, my header labels are black.
I see from this question that blue is default in Windows. I'd like to see the same effect in Linux. However, I have been unable to reverse the suggestions there. For instance, "foreground" does not appear to be an option for ttk.LabelFrame(), and I also don't know how to make the text of a Label to be blue to use it as the labelwidget option.
Here's the code:
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("My question")
lf = ttk.LabelFrame(root, text="Why is this not blue?")
lf.pack()
label = ttk.Label(lf, text="frame contents can remain black or whatever.")
label.pack()
root.mainloop()
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("My question")
lw = Label(root, text="This is blue now", fg="blue")
lf = ttk.LabelFrame(root,labelwidget=lw)
lf.pack()
label = ttk.Label(lf, text="frame contents can remain black or whatever.")
label.pack()
root.mainloop()
I wrote the answer but didn't notice you're using LabelFrame instead of Label.
As the documentation says, you need to pass an options argument to your LabelFrame class.
Try this:
lf = ttk.LabelFrame(root, fg='blue', text="Now it should be blue")
I am making a calculator using tkinter and I wish to do the following:
Disable keyboard input for the Entry widget so that the user can only input through the buttons.
Even after disabling keyboard input for the Entry widget, I wish to be able to change the background and foreground of the widget.
I wish to hide the console window because it is totally useless in the use of this calculator.
I don't want to let the user resize the root window. How do I disallow the resizing of the root window?
Here is my code so far...
from tkinter import *
root = Tk()
root.title("Calculator")
root.config(background="black")
operator = ""
textVar = StringVar()
def valInput(number):
global operator
operator+=str(number)
textVar.set(operator)
display = Entry(root, textvariable=textVar, font=("Arial", 14, "bold"), bg="lightblue", fg="black", justify="right")
display.grid(row=0, column=0, columnspan=4)
btn7 = Button(root, font=("Arial", 12, "bold"), bg="orange", fg="red", text="7", command= lambda : valInput(7))
btn7.grid(row=1, column=0)
"""
And more buttons...
"""
root.mainloop()
As you can see, I can input into the Entry widget using buttons but later on, after the calculator is complete, if the user inputs characters like abcd... it will cause problems and show errors. How do I disallow keyboard entry so that I can avoid these errors?
I want to make my calculator a bit colorful. I changed the color of the root window, the buttons and also the color of the Entry widget. Is there any way to change the color of the widget even after it is disabled?
I don't need the console window while using this calculator. How do I hide it?
If I resize the root window, the calculator becomes ugly, besides, resizing the window isn't necessary. So how do I prevent the user from resizing the window?
To be able to disable keyboard input in Entry(args)
Set the state to disabled:
display = Entry(root, state=DISABLED)
To be able to disable the feature of resizing the tkinter window (so that you can't drag and stretch it.
root.resizable(0,0)
To be able to make the command prompt window disappear. (I just want the tkinter window.
Rename the file with a .pyw extension (assuming you are using windows)
Don't use from tkinter import * it's really not recommended because it pollutes the main namespace with every public name in the module. At best this makes code less explicit, at worst, it can (and it will) cause name collisions.
Have the right reflexes, use import tkinter or import tkinter as tk instead
this should work, you have to use the disabledbackground option :
import tkinter as tk
root = tk.Tk()
display = tk.Entry(root,font=('Arial', 20, 'bold'), disabledbackground='lightblue', state='disabled')
display.pack()
root.resizable(0,0)
root.mainloop()
I have a ttk Entry which is in "disabled" state.
The background color of the entry field while disabled is a light blue shade.
How can i change it to the default grey color? From this post i understood how we can change the foreground color.
tkinter ttk Entry widget -disabledforeground
I tried the same method for background color and it did not work.
I am using python 2.7 in Windows 7.
This is the code i tried as per the above said post:
from Tkinter import *
from ttk import *
root=Tk()
style=Style()
style.map("TEntry",background=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')
root.mainloop()
You don't need to use styles. You can change the color of disabled entry with option disabledbackground=<color>. You can use this option when creating the entry , like:
entry.config(background="black",disabledbackground="red")
So you overall code(Example) is:
from tkinter import *
import time
root=Tk()
entry=Entry(root,state='disabled')
entry.config(background="black",disabledbackground="red")
entry.pack()
root.mainloop()
Here is a screenshot of the GUI:
In ttk and Tk entries widgets, background refers to different things. In Tk Entry, background refers to the color behind the text, in ttk entry, background refers to the color behind the widget. (Yes, I know, confusing right?), what you want to change is fieldbackground. So your code would be
from Tkinter import *
from ttk import *
root=Tk()
style=Style()
style.map("TEntry",fieldbackground=[("active", "black"), ("disabled", "red")])
entry_var=StringVar()
entry=Entry(root,textvariable=entry_var,state='disabled')
entry.pack()
entry_var.set('test')
root.mainloop()
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