Python_GUI
Python有許多GUI套件,有跟C結合的PyQt、跟Java結合的Jypthon、.net的Ironpython,以及內建的 Tkinter。新手推薦從Tkinter來作為開始,藉此學習GUI的程式架構、以及演算法程式如何與GUI結合,可以幫助在設計演算法時考慮兩者之間的關係,優化架構,新手如我就是run code的時候都很好,整合到GUI時才發現很多沒考慮到的部分需要修改,然後牽一髮動全身,差不多動了60%吧,200行的小程式就改的累死了,更何況是上千行的程式碼。Python_Tkinter
參考資料:http://www.java2s.com/Code/Python/GUI-Tk/CatalogGUI-Tk.htm
http://www.tkdocs.com/tutorial/index.html
http://www.tutorialspoint.com/python/python_gui_programming.htm
影片教學:(推薦看這個,很快就能上手了)
https://www.youtube.com/watch?v=RJB1Ek2Ko_Y&list=PL6gx4Cwl9DGBwibXFtPtflztSNPGuIB_d
Tkinter Widgets
Tkinter提供15種核心widgets以及ˋ4種進階工具,下列每個Operator都可以直接點連接查詢個別的控制參數以及更詳細的用法。
Operator | Description |
---|---|
The Button widget is used to display buttons in your application. | |
The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your application. | |
The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time. | |
The Entry widget is used to display a single-line text field for accepting values from a user. | |
The Frame widget is used as a container widget to organize other widgets. | |
The Label widget is used to provide a single-line caption for other widgets. It can also contain images. | |
The Listbox widget is used to provide a list of options to a user. | |
The Menubutton widget is used to display menus in your application. | |
The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton. | |
The Message widget is used to display multiline text fields for accepting values from a user. | |
The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time. | |
The Scale widget is used to provide a slider widget. | |
The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes. | |
The Text widget is used to display text in multiple lines. | |
The Toplevel widget is used to provide a separate window container. | |
The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values. | |
A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically. | |
A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts. | |
This module is used to display message boxes in your applications. |
Standard attributes
widget有下列基本屬性可以設定:Geometry Management
Tkinter 提供的widgets都具有下列三種方法 (methods) 來設定位置:- The pack() Method - This geometry manager organizes widgets in blocks before placing them in the parent widget.
- The grid() Method - This geometry manager organizes widgets in a table-like structure in the parent widget.
- The place() Method -This geometry manager organizes widgets by placing them in a specific position in the parent widget.
範例:
from tkinter import
*
root =
Tk() #主視窗
root.title("Element Number Analysis") #主視窗的名稱
frame_1
= Frame(root) #框架1(設置於主視窗root中)
frame_1.pack(side
= LEFT, fill = Y)
#pack:封裝至root中
#另一種寫法是 frame_1 = Frame(root).pack(side = LEFT, fill = Y)
#不過在某些情況會不行,若是編譯有Error時,
#不彷看看是不是這邊的問題。
#side = LEFT:fram1在root中的位置為左邊
#fill = Y :fram1填滿上下界
#其中控制參數都必須要大寫,ex:LEFT、Y
frame_2
= Frame(root) #框架2(設置於主視窗root中)
frame_2.pack(side
= LEFT, fill = Y)
frame_3
= Frame(root) #框架3(設置於主視窗root中)
frame_3.pack(side
= LEFT, fill = Y)
Scrollbar1
= Scrollbar(frame_1) #捲軸1(設置於框架1)
Scrollbar1.pack(
side = RIGHT, fill=Y )
#打包封裝至框架1中,且位置靠右側,並填滿上下界
Scrollbar2
= Scrollbar(frame_2) #捲軸1(設置於框架2)
Scrollbar2.pack(
side = RIGHT, fill=Y )
label_1 = Label(frame_1, text = '請輸入實施方式')
#標籤1(設置於框架1,標籤名稱為請輸入實施方式)
label_1.pack(side
= TOP) #打包封裝至框架1中,且位置靠上方
label_2 = Label(frame_2, text = '元件符號分析結果')
#標籤2(設置於框架2,標籤名稱為請輸入實施方式)
label_2.pack(side
= TOP) #打包封裝至框架2中,且位置靠上方
Retrive_input
= Text(frame_1,yscrollcommand = Scrollbar1.set)
#輸入文字欄Text(設置於框架1),
#yscrollcommand = Scrollbar1.set:在y軸設有捲軸1,
#根據捲軸1的控制參數可以知道捲軸會在框架1內的右側。
Retrive_input.pack(side
= RIGHT,fill = Y)
#打包封裝至框架1中,且位置靠右,但捲軸1已先設置於右側,
#所以Text會在其左側。
Scrollbar1.config(
command = Retrive_input.yview )
#前面只是讓捲軸1出現在視窗上而已,
#要讓捲軸1能動需加此命令 ,
#Retrive_input.yview :捲動捲軸1時文字欄是視窗做y軸方向移動。
Output_result
= Text(frame_2,yscrollcommand = Scrollbar2.set)
Output_result.pack(side
= RIGHT,fill = Y)
Scrollbar2.config(
command = Output_result.yview )
#同上
Button =
Button(frame_3, text="Analyze",
command=analyze)
#按鈕設置於框架3,標題為'Analyze',
#而Button的command對應的是一個函數 analyze,
#亦即按下Button後就會執行analyze。
Button.pack(side
= BOTTOM) #打包封裝Button至框架3,且位置在底部
root.mainloop() #進入訊息循環,才不會按完就跳掉。
若是要放置圖片,可以以label方式加入,先利用 PhotoImage(file= 'image.PNG') 讀檔,然後在label中加入控制參數 image = logo 即可,不過有圖片檔的腳本轉成exe檔比較複雜,無法直接用pyinstaller xxx.py來轉。
logo = PhotoImage(file= 'image.PNG')
label_3 = Label(frame_3, image = logo)
label_3.pack(side = BOTTOM)
label_3 = Label(frame_3, image = logo)
label_3.pack(side = BOTTOM)
沒有留言:
張貼留言