我正在嘗試使用繼承使以下代碼盡可能有意義。目的是為了學習。App 類是最超類的。Tabs 類繼承了它。我可以使以下代碼更有意義并打破它以獲得更多更好的類嗎?我不能在 Tab 類的init方法中初始化它,而不是為所有 Tabs 類方法創建 notebook = self.notebook 。完成后,筆記本無法識別。我需要所有標簽的背景顏色相同。因此,如果我可以在 Tabs 類的 init 方法中提及它并將其其他方法(about,...,visualize)轉換為 Tabs 類的子類,這會是一個好建議嗎?好心的幫助
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
from tkinter.font import Font
class App(tk.Tk):
def __init__(self):
super().__init__()
# intializing the window
self.title("Data Visualization")
# configuring size of the window
self.geometry('800x650')
# this removes the maximize button
self.resizable(0,0)
# Styling the tabs
s = ttk.Style()
s.theme_create('pastel', settings={
".": {
"configure": {
"background": '#ffffff', # All except tabs
"font": 'red'
}
},
"TNotebook": {
"configure": {
"background":'#848a98', # Your margin color
"tabmargins": [5, 5, 4, 4], # margins: left, top, right, separator
}
},
"TNotebook.Tab": {
"configure": {
"background": '#d9ffcc', # tab color when not selected
"padding": [10, 2], # [space between text and horizontal tab-button border, space between text and vertical tab_button border]
"font":"white"
},
"map": {
"background": [("selected", '#ccffff')], # Tab color when selected
"expand": [("selected", [1, 1, 1, 0])] # text margins
}
}
})
s.theme_use('pastel')
#s.theme_use('default')
s.configure('TNotebook.Tab', font=('URW Gothic L','13','bold'))
#s.map("TNotebook", background= [("selected", "#ffffff")])
#Create Tab Control
self.notebook = ttk.Notebook(self)
class Tabs(App):
def __init__(self):
super().__init__()
def about(self):
my_font = Font(
family = 'Arial',
size = 15,
weight = 'bold',
slant = 'roman',
underline = 0,
overstrike = 0
)
my_font2 = Font(
family = 'Arial',
size = 11,
#weight = 'bold',
slant = 'roman',
underline = 0,
overstrike = 0
)
notebook = self.notebook
f1 = tk.Frame(notebook)#,background="#FFFAF0")
#logo
logo = Image.open('airport.jpg')
#Resize the Image using resize method
resized_image= logo.resize((600,300), Image.ANTIALIAS)
logo = ImageTk.PhotoImage(resized_image)
logo_label = ttk.Label(f1,image=logo,relief="raised")
logo_label.image = logo
#logo_label.grid(column=3, row=0)
logo_label.place(relx=0.12,rely=0.1) # using place
notebook.add(f1, text="About" )
# Tab1
ttk.Label(f1, text="Airports, Airport-frequencies and Runways analysis", font=my_font).place(relx=0.2,rely=0.03)
#text box
text_box = tk.Text(f1, height =10,font=my_font2)
text_box.insert(1.0,"""This application allows you to analyze Airports, Airport-frequencies and Runways of Europe.
? Tab "Load and Save Actions" is to load the initial data set (which consists of three CSV files) and translate it into a suitable format. \n\n? Tab "Preprocess" is to clean and prepare the initial data set, managing inconsistences, \nerrors, missing values and any specific changes required. \n\n? Tab "Visualize" is to use the prepared data set to generate output and visualisations.""" )
text_box.tag_configure("center", justify="center")
text_box.tag_add("center", 1.0, "end")
text_box.place(relx=0.1, rely=0.65)
text_box.config(highlightthickness = 2, borderwidth=0,background='#FFFAFA')
notebook.pack(expand=1, fill="both")
def load_save(self):
notebook = self.notebook
f2 = tk.Frame(notebook,background="#ffffff")
notebook.add(f2, text="Load and Save Actions" )
def preprocess(self):
notebook = self.notebook
f3 = tk.Frame(notebook,background="#ffffff")
notebook.add(f3, text="Preprocess" )
def visualize(self):
notebook = self.notebook
f4 = tk.Frame(notebook,background="#ffffff")
notebook.add(f4, text="Visualize" )
if __name__ == "__main__":
tabs=Tabs()
tabs.about()
tabs.load_save()
tabs.preprocess()
tabs.visualize()
tabs.mainloop()
uj5u.com熱心網友回復:
在您的示例中,不應Tabs繼承自. 繼承是一種關系。如果繼承自,那意味著它是一個。意思是,你現在有兩個:原來的一個加上一個專門的版本。AppTabsAppTabs AppApps
這對于 tkinter 尤其成問題。Tkinter 被設計為Tk只有一個. 這將導致不直觀且通常不受歡迎的行為。AppApp TkTab
繼承不適用于在物件之間共享資料。如果Tabs需要訪問 中的資料或函式App,它應該通過呼叫函式或從App實體本身訪問資料來實作。
更好的結構是為選項卡創建自定義類,然后創建該類的實體,而不是使用創建選項卡的函式。
這是一個將 的實體app傳遞給每個選項卡的示例。然后,該選項卡可self.app用于呼叫函式或訪問App. 該類Tab將創建一個app.notebook帶有一些常用選項的框架。
通過使每個選項卡成為一個類,它為您提供了一種方便的方法來封裝特定于每個選項卡的函式和資料。
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
...
self.notebook = ttk.Notebook(self)
self.notebook.pack(fill="both", expand=True)
self.about_tab = AboutTab(app=self)
self.load_save = LoadSaveTab(app=self)
class Tab(tk.Frame):
def __init__(self, app, label):
# given an app, make this a child of app.notebook
self.app = app
super().__init__(app.notebook, background="#FFFAF0")
self.app.notebook.add(self, text=label)
class AboutTab(Tab):
def __init__(self, app):
super().__init__(app, "About")
label = tk.Label(self, text="This is the 'About' tab")
label.pack(fill="both")
...
class LoadSaveTab(Tab):
def __init__(self, app):
super().__init__(app, "Load and Save")
label = tk.Label(self, text="This is the 'Load and Save' tab")
label.pack(fill="both")
...
if __name__ == "__main__":
app = App()
app.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462683.html
