1.创建从一个列表框选择内容添加到另一个列表框
from tkinter import *
root = Tk()
def callbutton1():
for i in listb.curselection():
listb2.insert(0,listb.get(i))
def callbutton2():
for i in listb2.curselection():
listb2.delete(i)
li = ['c','python','php','html','SQL','java']
listb = Listbox(root)
listb2 = Listbox(root)
for item in li:
listb.insert(0,item)
listb.grid(row = 0,column = 0,rowspan = 2)
b1 = Button(root,text = '添加>>',command = callbutton1,width = 20)
b2 = Button(root,text = '删除<<',command = callbutton2,width = 20)
b1.grid(row = 0,column = 1,rowspan = 2)
b2.grid(row = 1,column = 1,rowspan = 2)
listb2.grid(row = 0,column = 2,rowspan = 2)
root.mainloop()
结果如下:
2.演示各消息框的程序
import tkinter as tk
from tkinter import messagebox as msgbox
def btn1_clicked():
msgbox.showinfo("Info","Showinfo test.")
def btn2_clicked():
msgbox.showinfo("Warning","Showwarning test.")
def btn3_clicked():
msgbox.showinfo("Error","Showerror test.")
def btn4_clicked():
msgbox.showinfo("Question","Askquestion test.")
def btn5_clicked():
msgbox.showinfo("OKCancel","Askookcancel test.")
def btn6_clicked():
msgbox.showinfo("YesNo","Askyesno test.")
def btn7_clicked():
msgbox.showinfo("Retry","Askretrycancel test.")
root = tk.Tk()
root.title("MsgBox Test")
btn1 = tk.Button(root,text = "showinfo",command = btn1_clicked)
btn1.pack(fill = tk.X) #X为大写
btn2 = tk.Button(root,text = "showwarning",command = btn2_clicked)
btn2.pack(fill = tk.X)
btn3 = tk.Button(root,text = "showerror",command = btn3_clicked)
btn3.pack(fill = tk.X)
btn4 = tk.Button(root,text = "askquestion",command = btn4_clicked)
btn4.pack(fill = tk.X)
btn5 = tk.Button(root,text = "askokcancel",command = btn5_clicked)
btn5.pack(fill = tk.X)
btn6 = tk.Button(root,text = "askyesno",command = btn6_clicked)
btn6.pack(fill = tk.X)
btn7 = tk.Button(root,text = "askretrycancel",command = btn7_clicked)
btn7.pack(fill = tk.X)
root.mainloop()