How to change the theme of my tkinter application in Python? - python

I'm trying to change my Tkinter theme but when I change s.theme_use('classic') to s.theme_use('calm') or s.theme_use('winnative') nothing changes.
Here is my code:
from tkinter import *
import tkinter.ttk as ttk
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
list_themes = s.theme_names()
current_theme = s.theme_use()
s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# These are the option menus
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window, dd_owner, *owner)
w.grid(row=0, column=1)
#The run button
run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()

Below is an modified example using ttk widgets based on your code:
from tkinter import *
import tkinter.ttk as ttk
import random
window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # geo of the window
s=ttk.Style()
s.configure("TButton", foreground="red", background="blue")
list_themes = s.theme_names()
current_theme = s.theme_use()
#s.theme_use('classic')
print(list_themes)
def run():
if dd_owner.get() == "Spain":
print("spain")
# choose a theme randomly
theme = random.choice(list_themes)
print("theme:", theme)
s.theme_use(theme)
# These are the option menus
owner = ("Spain", "France", "Germany")
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
#w = OptionMenu(window, dd_owner, *owner)
# use ttk.Combobox instead of OptionMenu
w = ttk.Combobox(window, textvariable=dd_owner, values=owner)
w.grid(row=0, column=1)
#The run button
#run_button = Button(window, text="Run application {}".format(dd_owner.get()), bg="blue", fg="white",command=run)
# use ttk.Button
run_button = ttk.Button(window, text="Run application {}".format(dd_owner.get()), command=run)
run_button.grid(column=0, row=2)
# These are the titles
l1 = ttk.Label(window, text='Select Owner', width=15)
l1.grid(row=0, column=0)
mainloop()
When you click the button, it will change the theme randomly.

Related

How should I change my tkinter code to rearrange the elements on my page?

I'm learning tkinter and getting stumped in one area. Here's the code:
from tkinter import *
from tkinter.messagebox import showinfo
def button_press():
showinfo('info','pressed button')
root = Tk()
root.geometry('800x500')
f = Frame(root)
f.pack()
Label(f, text="this is a line of text").pack(side=LEFT)
s = StringVar(value='enter here')
Entry(f, textvariable=s, width=100).pack(side=LEFT)
Button(f, text='Button', command=button_press).pack(side=RIGHT)
root.mainloop()
It produces:
But I want to align the text vertically with the entry field like this:
What do I need to change to make that happen?
Using .pack() is not advisable if you want to create more complex Frame structures.
Instead assign the items to variables and place those into a .grid().
.grid splits up your frame into different rows and columns or "sticks" them on a certain place.
below an example:
from tkinter import *
from tkinter.messagebox import showinfo
def button_press():
showinfo('info', 'pressed button')
root = Tk()
root.geometry('800x500')
f = Frame(root)
f.pack()
l1 = Label(f, text="this is a line of text")
l1.grid(row=1, column=1, sticky=W)
s = StringVar(value='enter here')
entry = Entry(f, textvariable=s, width=100)
entry.grid(row=2, column=1)
button = Button(f, text='Button', command=button_press)
button.grid(row=2, column=2)
root.mainloop()

How to reset the window in Tkinter based on a radio button selection

Im trying to create a Python Tkinter programme that resets the window based on a radio button selection. So that the user sees the correct input field(s) for the radio button selected. In the example below the user select a radiobutton to enter a band name (e.g. Duran Duran) or artist name in to 2 fields (e.g. Michael Kiwanuka).
The problem is that the tk windows just keep spawning new windows.
What I really want to understand is how can I create a window, add some widgets and then change the widgets depending on the radio button choice.
My code is:
from tkinter import *
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
def make_window():
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
def reset_all():
window.destroy()
make_window()
def artist_band():
print("Artist is a Band")
reset_all()
make_window()
inp_band_name = Entry(borderwidth=2, background="white", width=40, fg="red")
inp_band_name.insert(END, string="")
inp_band_name.pack()
def artist_person():
print("Artist is a Person")
reset_all()
make_window()
inp_first_name = Entry(borderwidth=2, background="white", width=25, )
inp_first_name.insert(END, string="")
inp_first_name.pack()
inp_second_name = Entry(borderwidth=2, background="white", width=25, )
inp_second_name.insert(END, string="")
inp_second_name.pack()
radio_state = IntVar()
radiobutton1 = Radiobutton(text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
window.mainloop()
window = Tk() should be the only one in the program. It can be minimized or hidden, but when it is closed, the program also ends. Child windows are created using Toplevel().
from tkinter import *
def make_window(text):
win = Toplevel()
win.title(text)
# parent_window.geometry("width_size x height_size + x_position + y_position")
win.minsize(width=850, height=550)
win.configure(bg="#bcbcbc")
name = Entry(win, borderwidth=2, background="white", width=40, fg="red")
name.insert(END, string=text)
name.pack()
window.iconify()
def reset_all():
window.destroy()
make_window()
def artist_band():
print("Artist is a Band")
make_window('Artist is a Band')
def artist_person():
print("Artist is a Person")
make_window('Artist is a Person')
window = Tk()
window.title("My Project")
# parent_window.geometry("width_size x height_size + x_position + y_position")
window.minsize(width=850, height=550)
window.configure(bg="#bcbcbc")
radio_state = IntVar()
radiobutton1 = Radiobutton(window, text="Artist is a Band", value=1, variable=radio_state, command=artist_band)
radiobutton2 = Radiobutton(window, text="Artist is a Person", value=2, variable=radio_state, command=artist_person)
radiobutton1.pack()
radiobutton2.pack()
window.mainloop()
Windows and Dialogs

Python - Tkinter - invisible text in ttk.Entry in sub window

I'm trying to make a sub window for configuration settings. But the text in the sub window is invisible. I am reading correctly but I cannot see the text. Below is my sample code with the problem:
import tkinter as tk
from tkinter import ttk
from tkinter import Menu
class Frames:
def __init__(self):
self.port_com = None
def main_frame(self, win):
# Main Frame
main = ttk.LabelFrame(win, text="")
main.grid(column=0, row=0, sticky="WENS", padx=10, pady=10)
return main
def dut_configuration_frame(self, win):
# Configuration Frame
dut_config_frame = ttk.LabelFrame(win, text="Config")
dut_config_frame.grid(column=0, row=0, sticky='NWS')
# Port COM
ttk.Label(dut_config_frame, text="Port COM").grid(column=0, row=0)
self.port_com = tk.StringVar()
ttk.Entry(dut_config_frame, width=12, textvariable=self.port_com).grid(column=0, row=1, sticky=tk.EW)
self.port_com.set(value="COM7")
print(self.port_com.get())
class ConfigFrames:
def __init__(self):
self.port_com = None
def main_frame(self, win):
# Main Frame
main = ttk.LabelFrame(win, text="")
main.grid(column=0, row=0, sticky="WENS", padx=10, pady=10)
return main
def configuration_frame(self, win):
# Configuration Frame
dut_config_frame = ttk.LabelFrame(win, text="Config")
dut_config_frame.grid(column=0, row=0, sticky='NWS')
# Port COM
ttk.Label(dut_config_frame, text="Port COM").grid(column=0, row=0)
self.port_com = tk.StringVar()
ttk.Entry(dut_config_frame, width=12, textvariable=self.port_com).grid(column=0, row=1, sticky=tk.EW)
self.port_com.set(value="COM5")
print(self.port_com.get())
def menu_bar(win):
def _config():
config_frame = ConfigFrames()
config_window = tk.Tk()
config_window.title("Sub window")
config_window.geometry("200x200")
config_window.resizable(0, 0)
main = config_frame.main_frame(config_window)
config_frame.configuration_frame(main)
config_window.mainloop()
# Menu
menuBar = Menu(win)
win.config(menu=menuBar)
settingsMenu = Menu(menuBar, tearoff=0)
settingsMenu.add_command(label="Config", command=_config)
menuBar.add_cascade(label="Settings", menu=settingsMenu)
frames = Frames()
win = tk.Tk()
win.title("Main window")
win.geometry("200x200")
win.resizable(0, 0)
menu_bar(win)
main = frames.main_frame(win)
frames.dut_configuration_frame(win)
win.mainloop()
As you can see in main window it is visible, but in sub window it is invisible.
And printing in console is correct:

how to display to action of each combo box value in single tkinter window using python

I write the following code:
from tkinter import *
from tkinter import Tk, Label, Frame, Entry, Button, StringVar
from tkinter.ttk import Combobox
root = Tk()
paper = StringVar()
def main_fn(event):
value = event.widget.get()
if(value=="1"):
button = Button(root,text = "Button 1",font="rockwell", bg=
"pale goldenrod")
button.grid(row=2,column=1,pady=5,ipadx=5,sticky=(E+W))
button2 = Button(root,text="Button 2", font="rockwell",
bg= "pale goldenrod")
button2.grid(row=3,column=1,pady=5,sticky=(E+W))
button3 = Button(root, text ="Button 3",font="rockwell",
bg ="pale goldenrod")
button3.grid(row=4,column=1,pady=5,sticky=(W+E))
button4 = Button(root, text="Button 4",font="rockwell",
bg="pale goldenrod")
button4.grid(row=5,column=1,pady=5,sticky=(W+E))
elif(value=="2"):
button = Button(root,text = "Button 1",font="rockwell", bg=
"pale goldenrod")
button.grid(row=2,column=1,pady=5,ipadx=5,sticky=(E+W))
button2 = Button(root,text="Button 2", font="rockwell",
bg= "pale goldenrod")
button2.grid(row=3,column=1,pady=5,sticky=(E+W))
label = Label(root,text="Select your choice",font="rockwell",relief="ridge",
bg="pale goldenrod")
label.grid(row=1,column=1,pady=5,sticky=(E+W),ipadx=5)
choose_np = Combobox(root,textvariable = paper,font=("rockwell",11))
choose_np['values']= ('1','2','3','4')
choose_np.grid(row=1,column=2,padx=5,sticky=(W+E),ipadx=5)
choose_np.current()
choose_np.bind("<<ComboboxSelected>>" , main_fn)
root.mainloop()
I want to perform some action on each combo box value, but the result of each value should be displayed on single window. If user select the value 1, the the result of 1 should be displayed on root window and again if user select value 2 then the result of the value 2 should be displayed on the same window and the result of value 1 should be cleared from the window.
So, only the result of the selected value should be displayed on a single window.
Okay, so i am come up with a solution. A frame can be used to display different content on different value selected.
from tkinter import *
from tkinter import Tk, Label, Frame, Entry, Button, StringVar
from tkinter.ttk import Combobox
root = Tk()
frame = Frame(root)
frame.grid(row=2,column=1)
paper = StringVar()
def main_fn(event):
value = event.widget.get()
if(value=="1"):
for widget in frame.winfo_children():
widget.destroy()
button = Button(frame,text = "Button 1",font="rockwell", bg=
"pale goldenrod")
button.grid(row=2,column=1,pady=5,ipadx=5,sticky=(E+W))
button2 = Button(frame,text="Button 2", font="rockwell",
bg= "pale goldenrod")
button2.grid(row=3,column=1,pady=5,sticky=(E+W))
button3 = Button(frame, text ="Button 3",font="rockwell",
bg ="pale goldenrod")
button3.grid(row=4,column=1,pady=5,sticky=(W+E))
button4 = Button(frame, text="Button 4",font="rockwell",
bg="pale goldenrod")
button4.grid(row=5,column=1,pady=5,sticky=(W+E))
elif(value=="2"):
for widget in frame.winfo_children():
widget.destroy()
button = Button(frame,text = "Button 1",font="rockwell", bg=
"pale goldenrod")
button.grid(row=2,column=1,pady=5,ipadx=5,sticky=(E+W))
button2 = Button(frame,text="Button 2", font="rockwell",
bg= "pale goldenrod")
button2.grid(row=3,column=1,pady=5,sticky=(E+W))
elif(value=="3"):
for widget in frame.winfo_children():
widget.destroy()
label = Label(root,text="Select your choice",font="rockwell",relief="ridge",
bg="pale goldenrod")
label.grid(row=1,column=1,pady=5,sticky=(E+W),ipadx=5)
choose_np = Combobox(root,textvariable = paper,font=("rockwell",11))
choose_np['values']= ('1','2','3','4')
choose_np.grid(row=1,column=2,padx=5,sticky=(W+E),ipadx=5)
choose_np.current()
choose_np.bind("<<ComboboxSelected>>" , main_fn)
root.mainloop()
Add a frame in root window where you want to change the content. widget.destroy() is used to clear the content of previous value selected.

Python canvas colour change by button

I am a python self learner. I was stuck on some practice.
My idea was to create a pop out GUI with buttons that can change the canvas colour.
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas = Canvas(root, bg = "red", height=100, width=100)
canvas.grid(row=0,column=2)
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
root.configure(background='white')
root.mainloop()
i haven't put in the green and blue button command yet, but instead of creating a new canvas when the colour button clicked, i just wanted to have the default canvas colour change.
Any help will be much appreciated!
Thanks in advance.
I think this is what you need -
from Tkinter import *
import ttk
import tkMessageBox
root = Tk()
root.title("Colour!")
canvasColor = "yellow"
def buttonRed() :
canvas.config(background="red")
def buttonGreen() :
canvas.config(background="green")
def buttonBlue() :
canvas.config(background="blue")
button = ttk.Button(root, text="Red", command = buttonRed)
button.grid(row=2,column=1)
button2 = ttk.Button(root, text ="Green", command = buttonGreen)
button2.grid(row=2,column=2)
button3 = ttk.Button(root, text="Blue", command = buttonBlue)
button3.grid(row=2,column=3)
canvas = Canvas(root, bg = canvasColor, height=200, width=200)
canvas.grid(row=0,column=2)
#canvas.config(background="black")
root.configure(background='white')
root.mainloop()

Categories

Resources