在Button的command绑定中,如果给的一个函数,则表示执行一次。
import tkinter as Tk
from tkinter import messagebox
i = 0
def myLabel():
global root,i
s = Tk.Label(root,text="the import thing is :" )
s.pack()
root = Tk.Tk()
def fun1():
if messagebox.showwarning("hello","你确定要离开吗?"):
root.destroy()
def fun2():
if messagebox.showwarning("hello","just is hello"):
root.destroy()
w1 = Tk.Label(root,text="欢迎来到我的世界")
w2 = Tk.Label(root,text="好好学习,天天向上",background="white")
w3 = Tk.Label(root,text="轻松愉快,就选python!",background="red")
w1.pack()
w2.pack()
w3.pack()
b = Tk.Button(root,text="Python",command = myLabel())
b.pack()
root.protocol('WM_DELETE_WINDOW',fun1)
root.mainloop()
此时表示执行myLabel()函数一次,在点击Button时不在进行调用,正确的写法是去掉括号
Button绑定的两种方法:
1、第一种是方法绑定事件
b = Tk.Button(root,text="Python",command = myLabel) b.pack()
2、第二种方法绑定事件
b = Tk.Button(py, text = "Python") # command = myLabel ⽊有了
b.bind("<Button-1>", myLabel) # 多了这⼀句
使用grid布局:
import tkinter as Tk
from tkinter import messagebox
root = Tk.Tk()
Tk.Label(root,text="account:").grid(row=0,sticky="w")
Tk.Entry(root).grid(row=0,column=1,sticky="e")
Tk.Label(root,text="password:").grid(row=1,sticky="w")
Tk.Entry(root).grid(row = 1,column = 1,sticky="e")
root.mainloop()
使用Lable标签模拟Button标签
import tkinter as Tk
from tkinter import messagebox
def myLabel(event):
global root
s = Tk.Label(root,text="好好学习,天天向上")
s.pack()
root = Tk.Tk()
n = Tk.Label(root,text="button")
n.bind("<Button-1>",myLabel)
n.pack()
root.mainloop()
简单的登录系统:
import tkinter as Tk
from tkinter import messagebox
def reg():
accout_get1 = accout_get.get()
password_get1 = password_get.get()
if accout_get1 == "liyong" and password_get1 == "102520":
msg_label["text"] = "load succecced!"
else:
msg_label["text"] = "load fail!"
root = Tk.Tk()
s = Tk.Label(root,text="ok")
accout_label = Tk.Label(root,text="accout:")
accout_label.grid(row=0,sticky="w")
accout_get = Tk.Entry(root)
accout_get.grid(row=0,column=1,sticky="e")
password_label = Tk.Label(root,text="password:")
password_label.grid(row=1,column=0,sticky="w")
password_get = Tk.Entry(root)
password_get.grid(row=1,column=1,sticky="e")
button_load = Tk.Button(root,text="load",command=reg)
#button_load.bind("Button-1",reg)
button_load.grid(row=2,column=1,sticky="e")
msg_label = Tk.Label(root,text=" ")
msg_label.grid(row=3)
root.mainloop()
其中msg_label的文件应该是通过什么方式进行了重写,
msg_label["text"] = "load succecced!"
这里的Label属性实现了动态的展示
菜单的简单添加:
import tkinter as Tk
from tkinter import messagebox
root = Tk.Tk()
menuBar = Tk.Menu(root)
for item in ["文件","编辑","视图","关于"]:
menuBar.add_command(label = item)
root["menu"] = menuBar
root.mainloop()
这⾥要注意的是我们可以先把⼦菜单做好,然后再做上层菜单。
import tkinter as Tk
from tkinter import messagebox
root = Tk.Tk()
menuBar = Tk.Menu(root)
fMenu = Tk.Menu(menuBar)
for item in ["新建","打开","保存","另存为","退出"]:
fMenu.add_command(label = item)
eMenu = Tk.Menu(menuBar)
for item in ["复制","粘贴","剪切","撤销"]:
eMenu.add_command(label = item)
vMenu = Tk.Menu(menuBar)
for item in ["默认视图","全屏模式","显示/隐藏菜单"]:
vMenu.add_command(label = item)
aMenu = Tk.Menu(menuBar)
for item in ["版权信息","帮助文档"]:
aMenu.add_command(label = item)
menuBar.add_cascade(label = "文件", menu = fMenu)
menuBar.add_cascade(label = "编辑", menu = eMenu)
menuBar.add_cascade(label = "视图", menu = vMenu)
menuBar.add_cascade(label = "关于", menu = aMenu)
root["menu"] = menuBar
root.mainloop()
弹出菜单的制作:
import tkinter as Tk
from tkinter import messagebox
def myLabel():
global root
Tk.Label(root,text="python !!!").pack()
root = Tk.Tk()
menuBar = Tk.Menu(root)
for item in ["C/C++","JavaEE","Android","PHP","UI","IOS","JS","could"]:
menuBar.add_command(label = item)
menuBar.add_command(label = "python",command = myLabel)
def pop(event):
menuBar.post(event.x_root,event.y_root)
root.bind("<Button-3>",pop)
root.mainloop()
radiobutton:
import tkinter as Tk
from tkinter import messagebox
root = Tk.Tk()
menuBar = Tk.Menu(root)
sonBar = Tk.Menu(menuBar)
item = ["语文","数学","英语","逻辑","科学","心理","体育"]
for echa in item:
sonBar.add_radiobutton(label = echa)
menuBar.add_cascade(label = "课程",menu = sonBar)
root["menu"] = menuBar
root.mainloop()
checkbutton
import tkinter as Tk
from tkinter import messagebox
root = Tk.Tk()
menuBar = Tk.Menu(root)
sonBar = Tk.Menu(menuBar)
items = ["语文","数学","英语"]
for item in items:
sonBar.add_checkbutton(label = item)
menuBar.add_cascade(label = "file",menu = sonBar)
root["menu"] = menuBar
root.mainloop()
总结: tkinter.messagebox的几种方法
showinfo
showwarning
showerror
askquestion
askyesno
askokcancel
askretrycancel
checkButton使用
import tkinter as Tk
from tkinter import messagebox
def fun1():
global lab
lab["text"] = "python 1 check"
root = Tk.Tk()
button1 = Tk.Checkbutton(root,text="python1",command = fun1)
#root.bind()
button1.pack()
lab = Tk.Label(root,text=" ")
lab.pack()
root.mainloop()
text文本使用
import tkinter as Tk
from tkinter import messagebox
root =Tk.Tk()
text = Tk.Text(root,height=30,widt=30)
text.pack()
root.mainloop()
Canvas
Toplevel
import tkinter as Tk
from tkinter import messagebox
root =Tk.Tk()
lb1 = Tk.Label(root,text="root label")
lb1.pack()
la2 = Tk.Toplevel(root,width=30,height=40)
#la2.pack()
root.mainloop()
button:
画象棋:
from tkinter import *
root = Tk()
root.title("中国象棋棋盘⼿绘")
canv = Canvas(root, width = 400, height = 450)
canv.create_line((0,2), (400,2), width = 2)
for i in range(10):
canv.create_line((0, i * 50), (400, i * 50), width = 2)
canv.create_line((3,0), (3,450), width = 2)
for i in range(8):
canv.create_line((i * 50,0), (i * 50,200), width = 2)
for i in range(8):
canv.create_line((i * 50,250), (i * 50,450), width = 2)
canv.create_line((397,0), (397,450), width = 2)
canv.create_line((150,0), (250,100), width = 2)
canv.create_line((150,100), (250,0), width = 2)
canv.create_line((150,450), (250,350), width = 2)
canv.create_line((150,350), (250,450), width = 2)
canv.create_text(110, 220, text = "汉界")
canv.create_text(290, 220, text = "楚河")
canv.pack()
root.mainloop()