基础代码:
import tkinter as tk windows = tk.Tk() windows.title(‘‘) # 设置主窗口标题 windows.geometry(‘100x100‘) # 像素大小 windows.resizable(0,0) #设置窗口大小不可调整 windows.mainloop() # 窗口循环
Button1=tk.Button(windows,text=‘‘,command = 按钮单击事件) Button1.place(height = 100,width = 100,x = 100,y = 100)
Listbox1 = tk.Listbox(windows,selectmode = EXTENDED)
Listbox1.place(height = 299,width = 370,x = 539,y = 116)
添加项目
Listbox1.insert(END,Text)
清空内容
Listbox1.delete(0,END)
添加滚动条(纵向)
Scrollbar2 = tk.Scrollbar(Listbox1) Scrollbar2.pack(side=RIGHT, fill=Y) Scrollbar2.config(command = Listbox1.yview)
获取选中内容(可以多选)
xuanzhong_index = Listbox1.curselection() for n in range(0,len(xuanzhong_index)): Text = Listbox1.get(xuanzhong_index[n]) print(Text)
获取选中内容(双击获取)
List1.bind(‘<Double-Button-1>‘,call_click) def call_click(event): text = list1.get(list1.curselection()) print(text)
1 from tkinter import ttk 2 columns1 = ("TITTLE","ID") 3 treeview1=ttk.Treeview(windows,height=10,show="headings",columns=columns1) 4 treeview1.place(height = 366,width = 546,x = 3,y = 47) 5 treeview1.column("TITTLE", width=300, anchor=‘center‘) 6 treeview1.column("ID", width=300, anchor=‘center‘) 7 treeview1.heading("TITTLE", text="TITTLE") # 显示表头 8 treeview1.heading("ID", text="ID")
获取选中内容(双击获取)
1 treeview1.bind(‘<Double-1>‘, 双击事件) # 绑定双击事件 2 for item in treeview1.selection(): 3 item_text = treeview1.item(item,‘values‘) 4 albumId = item_text[0] #获取第一个columns1的选中内容
添加项目
treeview1.insert(‘‘, ‘end‘,values=(title,id)) #向columns1添加内容
清空内容
def clear_list(tree): x=tree.get_children() for item in x: tree.delete(item) clear_list(treeview1)
Text1 = tk.Text(windows)
Text1.place(height = 88,width = 904,x = 5,y = 469)
插入内容
Text1.insert(END, ‘内容‘) Text1.see(END)
Entry1=tk.Entry(windows)
Entry1.place(height = 37,width = 160,x = 206,y = 5)
获取内容
text = Entry1.get()
加入内容
Entry1.insert(END,内容)
删除内容
Entry1.delete(0,END)
import tkinter.messagebox tkinter.messagebox.showinfo(‘提示‘,‘人生苦短‘) #提示 tkinter.messagebox.showwarning(‘警告‘,‘明日有大雨‘) #警告 tkinter.messagebox.showerror(‘错误‘,‘出错了‘) #错误
其他参考https://www.cnblogs.com/it-tsz/p/10582493.html
1 import tkinter.filedialog 2 path = tkinter.filedialog.askdirectory() #获取选中的文件夹目录 3 file = tkinter.filedialog.askopenfilename() #获取选中文件目录
其他内容等待更新.
原文:https://www.cnblogs.com/2020snow/p/12346554.html