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產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...