Python tkinter模組中類繼承的三種方式分析,pythontkinter

來源:互聯網
上載者:User

Python tkinter模組中類繼承的三種方式分析,pythontkinter

本文執行個體講述了Python tkinter模組中類繼承的三種方式。分享給大家供大家參考,具體如下:

tkinter class繼承有三種方式。

提醒注意這幾種繼承的運行方式

一、繼承 object

1.鋪tk.Frame給parent:

說明:

self.rootframe = tk.Frame(parent)
tk.Label(self.rootframe)

import tkinter as tkclass MyApp(object):  def __init__(self, parent):    self.rootframe = tk.Frame(parent)    self.rootframe.pack()    self.setupUI()  def setupUI(self):    tk.Label(self.rootframe, text='標籤').pack()if __name__ == '__main__':  root = tk.Tk()  MyApp(root) # 注意這句  root.mainloop()

2.直接使用root

說明:

self.root = parent
tk.Label(self.root)

import tkinter as tkclass MyApp(object):  def __init__(self, parent, **kwargs):    self.root = parent    self.root.config(**kwargs)    self.setupUI()  def setupUI(self):    tk.Label(self.root, text = '標籤').pack()if __name__ == '__main__':  root = tk.Tk()  app = test(root)  root.mainloop()

二、繼承 tk.Tk

import tkinter as tkclass MyApp(tk.Tk):  def __init__(self):    super().__init__()    self.setupUI()  def setupUI(self):    tk.Label(self, text='標籤').pack()if __name__ == '__main__':  MyApp().mainloop()

三、繼承 tk.Frame

分兩種情況

1.有parent

import tkinter as tkclass MyApp(tk.Frame):  def __init__(self, parent=None):    super().__init__(parent)    self.pack()    self.setupUI()  def setupUI(self):    tk.Label(self, text='標籤').pack()if __name__ == '__main__':  MyApp(tk.Tk()).mainloop()  #MyApp().mainloop() # 也可以這樣

注意: self.pack()

2.沒有parent

import tkinter as tkclass MyApp(tk.Frame):  def __init__(self):    super().__init__()    self.pack()    self.setupUI()  def setupUI(self):    tk.Label(self, text='標籤').pack()if __name__ == '__main__':   MyApp().mainloop()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.