Tkinter. Option button

Option button

basic concepts

The option button Radiobutton can be selected by clicking with the mouse, and only one option can be selected at a time. That is, the radio button.
Syntax format: Radiobutton (parent object, options,...)

Simple case

1. Gender selection

from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()
    def createWidget(self):
        #单选框
        self.v=StringVar()
        self.v.set("女")#默认选择女
        self.r1=Radiobutton(self,text="男性",value="男",variable=self.v)
        self.r2=Radiobutton(self,text="女性",value="女",variable=self.v)
        self.r1.pack(side="left")
        self.r2.pack(side="left")
        Button(self,text="确定",command=self.confirm).pack(side="left")
    def confirm(self):
        messagebox.showinfo("萤火虫寄语","选择的性别:"+self.v.get())
if __name__ == '__main__':
    root=Tk()
    root.geometry('500x300+200+200')
    root.title('萤火虫性别选择')
    app=Application(master=root)
    root.mainloop()

Insert picture description here
Insert picture description here
2. Choose your favorite place

from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()
    def createWidget(self):
        self.select_label=Label(self,text="最想去的地方").pack()
        self.address={
    
    
            0:"洱海",
            1:"稻城",
            2:"大理",
            3:"成都",
            4:"杭州"
        }
        self.v = IntVar()
        self.v.set(0)#默认索引为0即洱海
        for val,addre in self.address.items():
            Radiobutton(self,text=addre,variable=self.v,value=val).pack()
        Button(self,text="确定",width=10,command=self.confirm).pack(side="right")
    def confirm(self):
        messagebox.showinfo("萤火虫寄语","选择的地方为:"+self.address[self.v.get()])
if __name__ == '__main__':
    root=Tk()
    root.geometry('500x300+200+200')
    root.title('萤火虫性别选择')
    app=Application(master=root)
    root.mainloop()

Select Dali and click OK.
Insert picture description here
3. Box option button
Use the indication parameter in the Radiobutton method and set it to 0.

            Radiobutton(self,text=addre,indicatoron=0,variable=self.v,value=val).pack()

Insert picture description here
4. Picture button

from tkinter import *
from tkinter import messagebox
class Application(Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()
        self.createWidget()
    def createWidget(self):
        self.select_label=Label(self,text="天气情况").pack()
        self.image1=PhotoImage(file="g3.png")
        self.image2=PhotoImage(file="g4.png")
        self.image3=PhotoImage(file="g10.png")
        self.image4=PhotoImage(file="g11.png")

        self.v = StringVar()
        self.v.set("雨")
        self.rain=Radiobutton(self,image=self.image1,variable=self.v,value="雨",command=self.confirm).pack()
        self.snow=Radiobutton(self,image=self.image2,variable=self.v,value="雪",command=self.confirm).pack()
        self.trans_snow=Radiobutton(self,image=self.image3,variable=self.v,value="晴天有雪",command=self.confirm).pack()
        self.trans_rain=Radiobutton(self,image=self.image4,variable=self.v,value="晴天有雨",command=self.confirm).pack()
    def confirm(self):
        messagebox.showinfo("提示信息:","今天天气:"+self.v.get())
if __name__ == '__main__':
    root=Tk()
    root.geometry('500x900')
    root.title('萤火虫天气选择')
    app=Application(master=root)
    root.mainloop()

Insert picture description here
Choose the second
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/107963629