iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 2
1
自我挑戰組

軟體開發隨筆雜記--試著解決問題系列 第 2

如何以python tkinter完成一個最簡單的選單及分頁架構(1)

  • 分享至 

  • twitterImage
  •  

這一章帶大家繼續發展圖形介面的編排,我平常圖形介面喜歡用Notebook工具分割要展示的功能區塊,要使用Notebook分頁功能,必須帶入tkinter.ttk中的Notebook class, 在上一篇我已經在程式碼中先帶入了:

from tkinter.ttk import Notebook

先在__init__理假設我們先呼叫一個self.init_menubar(),
當然這個init_menubar()的域示定義在class內,也就是說算self的在下面我會設定它的內容,
接下來__init__內呼叫一個Notebook物件定義為self.notebook()
並定義在self上,self.notebook置放用pack方法,
內部參數side用tk.TOP靠上,fill表xy軸填滿,
expand表隨著母物件延伸,接下來我們再呼叫一個self.init_notebookpage_1();

class Test(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("This is a TEST!!!!")   	 
        self.geometry("240x200")
        self.init_menubar()
        self.notebook = Notebook(self)
        self.notebook.pack(side = tk.TOP,fill=tk.BOTH, expand=tk.YES)
        self.init_notebookpage_1()
        

我們寫init_menubar()的內部設定,menubar = tk.Menu(self):
呼叫tk.Menu元件,self.configure(menu=menubar)確認menu參數要顯示的項目為menubar,
functions = tk.Menu(menubar):functions為tk.Menu(menubar)的一項元件, functions.add_command(label="menu1", command = self.menu_1_command):
" menu1"為functions下一子項目,self.menu_1_command為可呼叫出來的子函式,

def menu_1_command(self, event = None):
     print("menu_1")

menubar.add_cascade(label="Functions", menu=functions): 設定functions顯現的Label為”Functions”;

def init_menubar(self):
    menubar = tk.Menu(self)
    self.configure(menu=menubar)
    functions = tk.Menu(menubar)
    functions.add_command(label="menu1", command = self.menu_1_command)
    menubar.add_cascade(label="Functions", menu=functions)

我又自訂一個self.init_notebookpage_1()函式,函式內容定義了一個 self.notebookpage_1_tab作為一個tk.Frame物件並讓self.notebook插入此物件為一頁。

def init_notebookpage_1(self):
    self.notebookpage_1_tab = tk.Frame(self.notebook)
    self.notebook.add(self.notebookpage_1_tab, text="notebookpage_1")

https://ithelp.ithome.com.tw/upload/images/20200914/20119608Tyx92WKjOF.png


上一篇
如何以python tkinter完成一個最簡單的GUI模板
下一篇
如何以python tkinter完成一個最簡單的選單及分頁架構(2)
系列文
軟體開發隨筆雜記--試著解決問題33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言