2022年1月7日 星期五

Python Tkinter –RadioButton

Python Tkinter –RadioButton

button = Radiobutton(master, text=”Name on Button”, variable = “shared variable”, value = “values of each button”, options = values, …)
shared variable = A Tkinter variable shared among all Radio buttons 
value = each radiobutton should have different value otherwise more than 1 radiobutton will get selected. 


#RadioButton in Tkinter

# Importing Tkinter module

from tkinter import *

# from tkinter.ttk import *


# Creating master Tkinter window

master = Tk()

master.geometry("320x320")


# Tkinter string variable

# able to store any string value

v = StringVar(master, "1")


# Dictionary to create multiple buttons

values = {"RadioButton 1" : "1",

"RadioButton 2" : "2",

"RadioButton 3" : "3",

"RadioButton 4" : "4",

"RadioButton 5" : "5"}


# Loop is used to create multiple Radiobuttons

# rather than creating each button separately

for (text, value) in values.items():

Radiobutton(master, text = text, variable = v,

value = value, indicator = 0,

background = "light blue").pack(fill = X, ipady = 5)


# Infinite loop can be terminated by

# keyboard or mouse interrupt

# or by any predefined function (destroy())

mainloop()


#==========================

# Importing Tkinter module

from tkinter import *

from tkinter.ttk import *


# Creating master Tkinter window

master = Tk()

master.geometry("175x175")


# Tkinter string variable

# able to store any string value

v = StringVar(master, "1")


# Dictionary to create multiple buttons

values = {"RadioButton 1" : "1",

"RadioButton 2" : "2",

"RadioButton 3" : "3",

"RadioButton 4" : "4",

"RadioButton 5" : "5"}


# Loop is used to create multiple Radiobuttons

# rather than creating each button separately

for (text, value) in values.items():

Radiobutton(master, text = text, variable = v,

value = value).pack(side = TOP, ipady = 5)


# Infinite loop can be terminated by

# keyboard or mouse interrupt

# or by any predefined function (destroy())

mainloop()

#==========================

# Importing Tkinter module

from tkinter import *

from tkinter.ttk import *


# Creating master Tkinter window

master = Tk()

master.geometry('175x175')


# Tkinter string variable

# able to store any string value

v = StringVar(master, "1")


# Style class to add style to Radiobutton

# it can be used to style any ttk widget

style = Style(master)

style.configure("TRadiobutton", background = "light green",

foreground = "red", font = ("arial", 10, "bold"))


# Dictionary to create multiple buttons

values = {"RadioButton 1" : "1",

"RadioButton 2" : "2",

"RadioButton 3" : "3",

"RadioButton 4" : "4",

"RadioButton 5" : "5"}


# Loop is used to create multiple Radiobuttons

# rather than creating each button separately

for (text, value) in values.items():

Radiobutton(master, text = text, variable = v,

value = value).pack(side = TOP, ipady = 5)


# Infinite loop can be terminated by

# keyboard or mouse interrupt

# or by any predefined function (destroy())

mainloop()




沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...