'''
python3.8.3
python自带tkinter开发图形界面
查询控件的属性用config()或keys(): Button().config()
anchor:锚定位
设置属性三种方式:
一、bt_ok = Button(self.window, fg='red', width=10)
二、bt_ok['text'] = '确定'
三、bt_ok.config(command= lambda :print("hi, everyone!"))
pack布局参数:
1) expand
控件位置:0/1 或 'no'/'yes',默认值0。
2) fill
x:横向填充,y:纵向填充,both:横向和纵向填充,none:无填充
3) ipadx,ipady
内边距。
4) padx,pady
控件与窗体的距离。
5) side
控件位置:TOP、BOTTOM、LEFT、RIGHT。
place布局参数:
1) anchor
控件方位:N、NE、E、SE、S、SW、W、NW或 CENTER。默认值是 NW,表示在左上角方位。
2) bordermode
控件的坐标是否要考虑边界的宽度:OUTSIDE 或 INSIDE,默认值是 INSIDE。
3) height
控件高度。
4) width
控件的宽度。
5) in(in_)
控件相对参考控件的位置。若使用在键值,则必须使用 in_。
6)relheight
控件相对于参考控件(使用 in_选项)的高度。
7) relwidth
控件相对于参考控件(使用 in_选项)的宽度。
8) relx
控件相对于参考控件(使用 in_选项)的水平位移。若没有设置 in_选项,则是相对于父控件。
9)rely
控件相对于参考控件(使用 in_选项)的垂直位移。若没有设置 in_选项,则是相对于父控件。
10) x
控件的绝对水平位置,默认值是 0。
11) y
控件的绝对垂直位置,默认值是 0。
'''
from tkinter import *
from tkinter import filedialog, messagebox
class tkinterTest():
window = Tk() # 创建窗口对象,只实例化一次
file_list = None
def __init__(self):
self.window.withdraw() # 隐藏窗口
self.addListbox()
self.addButton()
self.addMenu()
self.setWin()
self.window.mainloop() # 保持窗口显示
def setWin(self):
self.window.title('视频分享')
self.window.configure(bg='#008CBA')
self.window.iconbitmap('logo.ico')
# self.window.geometry("800x600")
# 居中
self.window.update_idletasks() # 检索窗口宽度和高度前调用,确保返回的值准确
window_width = 800
window_height = 600
screen_width = self.window.winfo_screenwidth()
screen_height = self.window.winfo_screenheight()
x = int((screen_width - window_width)/2)
y = int((screen_height - window_height)/2)
self.window.geometry(f"{window_width}x{window_height}+{x}+{y}")
self.window.deiconify() # 显示窗口
def addListbox(self):
self.file_list = Listbox(self.window, width=114, height=30)
self.file_list.insert(0, *['C','python','php','html','SQL','java'])
self.file_list.pack({"side": "top", 'anchor': 'n', 'expand': 1})
def addButton(self):
x, y = 300, 560
bt_text = ['确定', '删除']
for text in bt_text:
n = bt_text.index(text) # 防止n始终为最后一个值
Button(self.window, width=10, command=lambda n=n: self.buttonClick(n), text=text).place(x=x, y=y)
x += 100
def addMenu(self):
# top = self.window.winfo_toplevel()
# mu = Menu(top)
# top['menu'] = mu
# mu.add_cascade(label='添加文件夹', command=lambda :print('选择文件夹'))
mu = Menu(self.window)
mu.add_command(label="选择文件夹", command=lambda :self.selectFileFolder(1))
mu.add_command(label="选择文件", command=lambda :self.selectFileFolder(0))
mu.add_command(label="退出", command=self.window.quit)
self.window.config(menu=mu)
# 选择文件/文件夹
def selectFileFolder(self, stype):
fpath = filedialog.askdirectory() if stype else filedialog.askopenfilenames()
if fpath and len(fpath) > 0: self.file_list.insert("end", fpath)
def buttonClick(self, btype):
select_items = self.file_list.curselection()
if len(select_items) == 0:
messagebox.showinfo('info', '请先选择一条记录')
return
item_index = select_items[0]
if btype == 0:
messagebox.showinfo('info', self.file_list.get(item_index))
else:
self.file_list.delete(ACTIVE)
item_nums = self.file_list.size()
if item_nums > 0:
if item_index == item_nums:
item_index -= 1
self.file_list.selection_set(item_index)
tkinterTest()
结果: