简单组件学习 Radiobutton
单选按钮、Checkbutton
复选按钮和canvas
画布
文章目录
- `Radiobutton` 单选按钮
- `Checkbutton` 复选按钮
- `canvas` 画布
Radiobutton
单选按钮
Radiobutton
控件用于选择同一组单选按钮中的一个。Radiobutton
可以显示文本,也可以显示图像。
from tkinter import *
from tkinter import messagebox
class Application(Frame):def __init__(self, master=None):super().__init__(master) # super()代表的是父类的定义,而不是父类对象self.master = masterself.pack()self.createWidget()def createWidget(self):self.v = StringVar();self.v.set("F")self.r1 = Radiobutton(self, text="男性", value="M", variable=self.v)self.r2 = Radiobutton(self, text="女性", value="F", 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("400x50+200+300")app = Application(master=root)root.mainloop()
Checkbutton
复选按钮
Checkbutton
控件用于选择多个按钮的情况。Checkbutton
可以显示文本,也可以显示图像。
from tkinter import *
from tkinter import messagebox
class Application(Frame):def __init__(self, master=None):super().__init__(master) # super()代表的是父类的定义,而不是父类对象self.master = masterself.pack()self.createWidget()def createWidget(self):self.codeHobby = IntVar();self.videoHobby = IntVar()print(self.codeHobby.get()) # 默认值是0self.c1 = Checkbutton(self, text="敲代码",variable=self.codeHobby, onvalue=1, offvalue=0)self.c2 = Checkbutton(self, text="看视频",variable=self.videoHobby, 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.videoHobby.get() == 1:messagebox.showinfo("测试","看视频,都是正常人有的爱好!你喜欢看什么类型?")if self.codeHobby.get() == 1:messagebox.showinfo("测试","抓获野生程序猿一只,赶紧送去学习")if __name__ == '__main__':root = Tk()root.geometry("400x50+200+300")app = Application(master=root)root.mainloop()
canvas
画布
canvas
(画布)是一个矩形区域,可以放置图形、图像、组件等。我们简单介绍canvas 的使用,后续“图形绘制”会更加深入讲解。
from tkinter import *
from tkinter import messagebox
import random
class Application(Frame):def __init__(self, master=None):super().__init__(master) # super()代表的是父类的定义,而不是父类对象self.master = masterself.pack()self.createWidget()def createWidget(self):self.canvas = Canvas(self, width=300, height=200, bg="green")self.canvas.pack()# 画一条直线line = self.canvas.create_line(10, 10, 30, 20, 40, 50)# 画一个矩形.rect = self.canvas.create_rectangle(50, 50, 100, 100)# 画一个椭圆.坐标两双。为椭圆的边界矩形左上角和底部右下角oval = self.canvas.create_oval(50, 50, 100, 100)global photophoto = PhotoImage(file=r"F:\python_l\pythonProject1\60011.gif")self.canvas.create_image(150,170,image=photo)Button(self, text="画10 个矩形",command=self.draw50Recg).pack(side="left")def draw50Recg(self):for i in range(0, 10):x1 = random.randrange(int(self.canvas["width"])/2)y1 = random.randrange(int(self.canvas["height"])/2)x2 = x1 + random.randrange(int(self.canvas["width"])/2)y2 = y1 + random.randrange(int(self.canvas["height"])/2)self.canvas.create_rectangle(x1, y1, x2, y2)
if __name__ == '__main__':root = Tk()root.geometry("400x300+200+300")app = Application(master=root)root.mainloop()