import tkinter as tk from tkinter import filedialog, messagebox # 参考: https://www.cnblogs.com/shwee/p/9427975.html #根窗体就是画板,在tkinter中则是Toplevel,画布就是tkinter中的容器(Frame), # 画板上能够放不少张画布(Convas),tkinter中的容器中也能够放不少个容器, # 绘画中的构图布局则是tkinter中的布局管理器(几何管理器),绘画的内容就是tkinter中的一个个小组件 def gui(): # 建立窗口 window = tk.Tk() window.title('window title') # w*h window.geometry('1000x1000') # weight数据 click_count=None entry_input=None text=None list_items=None radio_button=None check_button=None # 更改label内容 def click(): click_count.set(int(click_count.get()) + 1) print(click_count.get()) #构建插件 click_count = tk.StringVar() click_count.set(0) # height=2,就是标签有2个字符这么高 # 动态显示文本,静态文本直接text='msg label = tk.Label(window,textvariable=click_count, font=('Arial', 12), width=30, height=2, bg='gray') #左上角位置坐标,label.pack()默认放在中间。 label.place(x=10, y=0) # 构建按钮 b = tk.Button(window, text='click', font=('Arial', 12), width=10, height=1, command=click) b.place(x=10, y=50) # 编辑text def insert(): input=entry_input.get() #在鼠标处插入,'end':在尾部插入 text.insert('insert',input) # x.y :第x行,第y列,行从1 开始,列从0开始。 # 1.2-->end print(text.get(1.2,'end')) # 单行文本输入域 # show='*' 密码格式,普通文本:show:None entry_input = tk.Entry(window, show='*', font=('Arial', 14)) entry_input.place(x=10, y=100) b = tk.Button(window, text='insert', font=('Arial', 12), width=10, height=1, command=insert) b.place(x=10, y=150) # 多行文本输入 text = tk.Text(window, height=3,width=20) text.place(x=10, y=200) # 得到选中项 def print_list_selection(): print(list_items.get(list_items.curselection())) # 构建下拉列表 var=tk.StringVar() var.set((1,2,3,4)) list_items = tk.Listbox(window, listvariable=var,height=3) # lb.insert(1, 'first') # 在第一个位置加入'first'字符 # lb.delete(2) #删除选项 list_items.place(x=10, y=250) v=tk.Button(window,text='show selected', width=15, height=1, command=print_list_selection) v.place(x=10, y=450) #输出单选框内容 def print_button_selection(): print(radio_button.get()) # 构建单选框 radio_button=tk.StringVar() r1 = tk.Radiobutton(window, text='A', variable=radio_button, value='A', command=print_button_selection) r1.place(x=10, y=500) r2 = tk.Radiobutton(window, text='B', variable=radio_button, value='B', command=print_button_selection) r2.place(x=10, y=550) # 输出确认项 def print_check_selection(): print(check_button.get()) # 构建check button check_button = tk.IntVar() c = tk.Checkbutton(window, text='check',variable=check_button, onvalue=1, offvalue=0, command=print_check_selection) # 传值原理相似于radiobutton部件 c.place(x=10, y=600) # 输出滑动条 # 默认传参v def print_scale_selection(v): print(v) # 构建滑动条 s = tk.Scale(window, label='scale msg', from_=0, to=10, orient=tk.HORIZONTAL, length=200, showvalue=1,tickinterval=2, resolution=0.1, command=print_scale_selection) s.place(x=10, y=650) # 构建帧 frame = tk.Frame(window,bg='green',height=300,width=300) frame.place(x=600,y=10) # 在帧上构建画布 canvas = tk.Canvas(frame, bg='red', height=200, width=200) # 说明图片位置,并导入图片到画布上 image_file = tk.PhotoImage(file='pic.gif') # 图片位置(相对路径,与.py文件同一文件夹下,也能够用绝对路径,须要给定图片具体绝对路径) image = canvas.create_image(0, 0, anchor='nw', image=image_file) # 图片锚定点(n图片顶端的中间点位置)放在画布(250,0)坐标处 canvas.place(x=10,y=10) # 文件操做 def OpenFile(): f = filedialog.askopenfilename(title='打开文件', filetypes=[('Python', '*.py *.pyw'), ('All Files', '*')]) print(f) # 可以使用os 模块运行文件 def SaveFile(): f = filedialog.asksaveasfilename(title='保存文件', initialdir='d:\mywork', initialfile='hello.py') print(f) def do_job(): print('do something') # 构建按钮栏 menubar = tk.Menu(window) # 菜单项(默认不下拉 tearoff=0,下拉内容包括Open,Save,Exit功能项) filemenu = tk.Menu(menubar, tearoff=0) # 将上面定义的空菜单命名为File,放在菜单栏中 menubar.add_cascade(label='File', menu=filemenu) # 在File中加入Open、Save等小菜单,即咱们平时看到的下拉菜单,每个小菜单对应命令操做。 filemenu.add_command(label='Open', command=OpenFile) filemenu.add_command(label='Save', command=SaveFile) # 添加一条分隔线 filemenu.add_separator() # 用tkinter里面自带的quit()函数 filemenu.add_command(label='Exit', command=window.quit) # 建立一个Edit菜单项(默认不下拉,下拉内容包括Cut,Copy,Paste功能项) editmenu = tk.Menu(menubar, tearoff=0) # 将上面定义的空菜单命名为 Edit,放在菜单栏中,就是装入那个容器中 menubar.add_cascade(label='Edit', menu=editmenu) # 一样的在 Edit 中加入Cut、Copy、Paste等小命令功能单元, editmenu.add_command(label='Cut', command=do_job) editmenu.add_command(label='Copy', command=do_job) editmenu.add_command(label='Paste', command=do_job) #建立第二级菜单,即菜单项里面的菜单 submenu = tk.Menu(filemenu) # 给放入的菜单submenu命名为Import filemenu.add_cascade(label='Import', menu=submenu, underline=0) # # 建立第三级菜单命令 submenu.add_command(label='Submenu', command=do_job) # 建立菜单栏完成后,配置让菜单栏menubar显示出来 window.config(menu=menubar) # grid 实现布局 # row为行,colum为列,padx就是单元格左右间距,pady就是单元格上下间距, # ipadx是单元格内部元素与单元格的左右间距,ipady是单元格内部元素与单元格的上下间距。 canvas = tk.Canvas(window, bg='gray', height=200, width=200) canvas.place(x=600,y=400) for i in range(3): for j in range(3): tk.Label(canvas, text=1).grid(row=i, column=j, padx=10, pady=10, ipadx=10, ipady=10) # 建立消息框 bool result = messagebox.askokcancel('Python Tkinter', 'ok/cancel') print(result) # str yes/no result = messagebox.askquestion('Python Tkinter', "yes/no?") print(result) # bool result = messagebox.askyesno('Python Tkinter', 'true/flase?') print(result) # 警告,错误 messagebox.showinfo('Python Tkinter', 'info') messagebox.showwarning('Python Tkinter', 'warning') messagebox.showerror('Python Tkinter', 'error') #建立一个全新的window window_up = tk.Toplevel(window) window_up.geometry('300x200') window_up.title('Sign up window') # window.mainloop就会让window不断的刷新,若是没有mainloop,就是一个静态的window # 一个gui程序只能有一个mainloop window.mainloop() if __name__=='__main__': gui()