Radiobutton Checkbutton 详解
- 1、一个经典的面向程序写法模块
- 2、单选框
-
- 2.1、单选组件(Radiobutton)
- 2.2、单选组件引用的函数
- 2.3、源代码
- 3、多选框
-
- 3.1、多选组件(Checkbutton)
- 3.2、单选组件引用的函数
- 3.3、源代码
1、一个经典的面向程序写法模块
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:创建组件
"""
......
......
if __name__ == "__main__":
window = Tk()
window.geometry("500x200+200+200")
window.title('一个经典的GUI程序')
app = Appication(master = window)
window.mainloop()
2、单选框
2.1、单选组件(Radiobutton)
def CreateWidget(self):
"""
用途:创建组件
"""
self.v = StringVar()
self.v.set("Female")
self.r1 = Radiobutton(self, text = "男性", value = "Male", variable = self.v)
self.r2 = Radiobutton(self, text = "女性", value = "Female", variable = self.v)
self.r1.pack(side = 'left');self.r2.pack(side = 'left')
Button(self, text = "确定", command = self.confirm).pack(side = 'left')
2.2、单选组件引用的函数
def confirm(self):
messagebox.showinfo("测试", "选择的性别:" + self.v.get())
2.3、源代码
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:创建组件
"""
self.v = StringVar()
self.v.set("Female")
self.r1 = Radiobutton(self, text = "男性", value = "Male", variable = self.v)
self.r2 = Radiobutton(self, text = "女性", value = "Female", 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__":
window = Tk()
window.geometry("500x200+200+200")
window.title('一个经典的GUI程序')
app = Appication(master = window)
window.mainloop()
3、多选框
3.1、多选组件(Checkbutton)
def CreateWidget(self):
"""
用途:创建组件
"""
self.codeHobby = IntVar()
self.dogHobby = IntVar()
print(self.codeHobby.get())
self.c1 = Checkbutton(self, text = "代码",
variable = self.codeHobby, onvalue = 1, offvalue = 0)
self.c2 = Checkbutton(self, text = "宠物狗",
variable = self.dogHobby, onvalue = 1, offvalue = 0)
self.c1.pack(side = "left");self.c2.pack(side = "left")
Button(self, text = "确定", command = self.confirm).pack(side = "left")
3.2、单选组件引用的函数
def confirm(self):
if self.codeHobby.get() == 1:
messagebox.showinfo("测试", "希望你作为程序员继续努力!")
if self.dogHobby.get() == 1:
messagebox.showinfo("测试", "希望你能和你的狗狗永远幸福!")
3.3、源代码
from tkinter import *
from tkinter import messagebox
class Appication(Frame):
"""
一个经典的 GUI 类的写法
"""
def __init__(self, master = None):
super().__init__(master)
self.master = master
self.pack()
self.CreateWidget()
def CreateWidget(self):
"""
用途:创建组件
"""
self.codeHobby = IntVar()
self.dogHobby = IntVar()
print(self.codeHobby.get())
self.c1 = Checkbutton(self, text = "代码",
variable = self.codeHobby, onvalue = 1, offvalue = 0)
self.c2 = Checkbutton(self, text = "宠物狗",
variable = self.dogHobby, onvalue = 1, offvalue = 0)
self.c1.pack(side = "left");self.c2.pack(side = "left")
Button(self, text = "确定", command = self.confirm).pack(side = "left")
def confirm(self):
if self.codeHobby.get() == 1:
messagebox.showinfo("测试", "希望你作为程序员继续努力!")
if self.dogHobby.get() == 1:
messagebox.showinfo("测试", "希望你能和你的狗狗永远幸福!")
if __name__ == "__main__":
window = Tk()
window.geometry("500x200+200+200")
window.title('一个经典的GUI程序')
app = Appication(master = window)
window.mainloop()