Python内置图形界面tkinter组件--入门2
Tkinter 组件
Button 按钮控件
import tkinter as tk
# 初始化窗口
main_win = tk.Tk()
# 窗口名
main_win.title("Tk window")
# 设置窗口大小
main_win.geometry("640x480")
# 将btn的内容设置为字符串类型,点击时改变内容
btn1_var = tk.StringVar()
btn1_var.set("Click me")
count = 0
def on_click():
global count
count += 1
btn1_var.set("number of clicks:" + str(count))
# 设置按钮并放置,传入on_click函数,点击时改变按钮内容
btn1 = tk.Button(main_win, textvariable=btn1_var, font=("Arial", 12), width=20, height=1, command=on_click)
btn1.pack()
# 设置固定文本的Button,不传点击函数
btn2 = tk.Button(main_win, text="Don't click me", activebackground="blue", font=("Arial", 12), width=20, height=1, command=None)
btn2.pack()
# 窗口持续显示,相当于一个while True
main_win.mainloop()
Canvas 画布控件
用于显示图形元素或文本。
import tkinter as tk
# 初始化窗口
main_win = tk.Tk()
# 窗口名
main_win.title("Tk window")
# 设置窗口大小
main_win.geometry("640x480")
canvas = tk.Canvas(main_win, bg="white", cursor="plus", height=400, width=640)
canvas.pack()
step = -1
def on_click():
global step
step += 1
min_x, min_y, max_x, max_y = 10, 10, 110, 110
canvas.create_rectangle(min_x+(max_x-min_x)*step, min_y, max_x+(max_x-min_x)*step, max_y )
# 设置按钮并放置,传入on_click函数
btn1 = tk.Button(main_win, text="create rectangle", font=("Arial", 12), width=20, height=1, command=on_click)
btn1.pack()
# 导入图片到画布上
image_file = tk.PhotoImage(file='../image/python.png') # 图片位置(相对路径,与.py文件同一文件夹下,也可以用绝对路径,需要给定图片具体绝对路径)
image = canvas.create_image(100, 120, anchor='n',image=image_file) # 图片锚定点(n图片顶端的中间点位置)放在画布(250,0)坐标处
# # 定义多边形参数,然后在画布上画出指定图形
x0, y0, x1, y1 = 210, 110, 260, 160
# 画直线
line = canvas.create_line(x0, y0, x1, y1)
# 画圆 填充颜色
oval = canvas.create_oval(x0+120, y0+50, x1+120, y1+50, fill='blue')
# 画扇形 从30度打开收到120度结束
arc = canvas.create_arc(x0, y0+50, x1, y1+50, start=30, extent=120)
# 窗口持续显示,相当于一个while True
main_win.mainloop()
Entry 和 Text 文本框控件
Entry单行文本输入框,相当于HTML5中的input
Text多行文本输入框,相当于HTML5中的textarea
import tkinter as tk
# 初始化窗口
main_win = tk.Tk()
# 窗口名
main_win.title("Tk window")
# 设置窗口大小
main_win.geometry("640x480")
entry=tk.Entry(main_win,bd=1)
entry.pack()
text = tk.Text(main_win, height=5)
text.pack()
def on_click():
text.insert('insert', entry.get()+"\n")
btn1 = tk.Button(main_win, text="Get entry text", font=("Arial", 12), width=30, height=1, command=on_click)
btn1.pack(side="bottom")
# 窗口持续显示,相当于一个while True
main_win.mainloop()
Label 标签控件
可以用来显示文本和位图
import tkinter as tk
from PIL import Image, ImageTk
# 初始化窗口
main_win = tk.Tk()
# 窗口名
main_win.title("Tk window")
# 设置窗口大小
main_win.geometry("640x480")
# 显示文字
label1 = tk.Label(main_win, text='可以用来显示文本和位图', bg='#999aaa', font=('Arial', 12), width=30, height=2)
label1.pack()
# 显示图片
image_file = tk.PhotoImage(file='../image/python.png')
label2 = tk.Label(main_win, image=image_file, bd=0)
label2.pack()
# 窗口持续显示,相当于一个while True
main_win.mainloop()
Radiobutton 单选框和 Checkbutton 复选框
import tkinter as tk
from PIL import Image, ImageTk
# 初始化窗口
main_win = tk.Tk()
# 窗口名
main_win.title("Tk window")
# 设置窗口大小
main_win.geometry("640x480")
label1 = tk.Label(main_win, text='可以用来显示文本和位图', bg='#999aaa', font=('Arial', 12), width=30, height=2)
label1.pack()
radio_var = tk.IntVar() # 定义一个var用来将radiobutton的值和Label的值联系在一起.
def change_color():
selected = radio_var.get()
if selected == 1:
label1["bg"] = "red"
elif selected == 2:
label1["bg"] = "green"
elif selected == 3:
label1["bg"] = "blue"
else:
label1["bg"] = '#999aaa'
# Radiobutton
r1 = tk.Radiobutton(main_win, text="Red", variable=radio_var, value=1, command=change_color)
r1.pack()
r2 = tk.Radiobutton(main_win, text="Green", variable=radio_var, value=2, command=change_color)
r2.pack()
r3 = tk.Radiobutton(main_win, text="Blue", variable=radio_var, value=3, command=change_color)
r3.pack()
check_var1 = tk.IntVar() # 定义var1和var2整型变量用来存放选择行为返回值
check_var2 = tk.IntVar()
def sel():
t = "我会"
if check_var1.get() == 1:
t += ' Python'
# label2.config(text = t)
if check_var2.get() == 1:
t += ' C++'
else:
t=""
label2.config(text = t)
# Checkbutton
c1 = tk.Checkbutton(main_win, text='Python',variable=check_var1, onvalue=1, offvalue=0, command=sel) # 传值原理类似于radiobutton部件
c1.pack()
c2 = tk.Checkbutton(main_win, text='C++',variable=check_var2, onvalue=1, offvalue=0, command=sel)
c2.pack()
label2 = tk.Label(main_win)
label2.pack()
# 窗口持续显示,相当于一个while True
main_win.mainloop()
这个选项长度不一致,没法对齐,着实恶心到我了,不知有什么办法可以对其呢?
今天就先折腾到这里吧~