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()