0. tkinter.Tk() 零基础建议从1开始看别从0
主窗口的一些方法
import tkinter as tk
root=tk.Tk()
def exit():
root.quit() #退出
def update():
root.update() #刷新界面
root.title('标题名称就叫这个')
root.geometry('250x150') #主窗体的大小
root.resizable(20,0) #框体大小可调性,分别表示x,y方向的可变性,第一个参数为x,第二个为y
bt1=tk.Button(root,text='exit',command=exit)
bt1.pack(side=tk.BOTTOM)
bt2=tk.Button(root,text='update',command=update)
bt2.pack(side=tk.BOTTOM)
tk.mainloop()
1. Lable Button
显示时间
from tkinter import *
import time
#给button按钮用的函数
def refresh_time():
timestring.set('%s'%time.ctime())
complexLabel.update()
root=Tk()
frame1=Frame(root)
frame1.pack()
frame2=Frame(root)
frame2.pack()
#Label
timestring=StringVar() #建立StringVar类对象
timestring.set('%s'%time.ctime()) #初始化StringVar对象的值
img=PhotoImage(file='theimg.png') #建立PhotoImage类对象
complexLabel=Label(frame1,
#text='天王盖地虎,\n小鸡炖蘑菇。', #文本
textvariable=timestring, #可变文本
image=img, #图像
justify=RIGHT, #对齐方式 每行文字都会向这个方向对齐,默认CENTER居中
anchor=None, #文字在Label框内的方位 N,S,E,W,NE,NW,SE,SW(东南西北)
compound=CENTER, #组合方式 LEFT图左文右,RIGHT图右文左,CENTER图文重合
height=360,width=640, #Label的宽高 单位:以系统默认的中文的一个字体宽高为单位
padx=20,pady=10, #边距 单位是像素
fg='blue',bg=None, #颜色 fg:字体颜色,bg:字体背景色
font=('华文行楷',35), #字体名+大小
relief='raised', #边框样式 flat,sunken,raised,ridge
bd=15 #边框大小
)
complexLabel.pack()
#Button
button=Button(frame2,
text='刷新时间',
image=None, #Button的大小根据图片大小确定
justify=None,
anchor=None,
compound=None, #这些在Button里同样适用
font=('华文行楷',15),
fg='red',bg=None,
width=20,height=1,
padx=10,pady=5,
bd=8,
relief='sunken', #flat groove ridge raised solid sunken
cursor='hand2' , #鼠标 有pencil,circle,hand1,hand2
command=refresh_time #点击时触发函数 传参数用lambda:函数(参数)的形式
)
button.pack()
mainloop()
效果图
2. Checkbutton Raidobutton
随便敲了个屑作
from tkinter import *
root=Tk()
v1=IntVar()
v2=IntVar()
v3=IntVar()
var=IntVar()
frame1=LabelFrame(root,bg='brown',text='Checkbutton部分',labelanchor=NE)
frame1.place(relx=0.2,rely=0.2,relwidth=0.3,relheight=0.3)
frame2=LabelFrame(root,bg='green',text='Raidobutton部分',labelanchor=NW)
frame2.place(relx=0.5,rely=0.5,relwidth=0.4,relheight=0.4)
check1=Checkbutton(frame1,text='流浪汉',variable=v1,fg='blue',bg='green')
check1.grid(row=0,column=0)
check2=Checkbutton(frame1,text='高中生',variable=v2,fg='black',bg='green')
check2.grid(row=1,column=0)
check3=Checkbutton(frame1,text='穿越者',variable=v3,fg='red',bg='green')
check3.grid(row=2,column=0)
radio1=Radiobutton(frame2,text='流浪汉',variable=var,value=1)
radio1.grid(row=0,column=0)
radio2=Radiobutton(frame2,text='掠夺者',variable=var,value=1)
radio2.grid(row=0,column=1)
radio3=Radiobutton(frame2,text='高中生',variable=var,va