第一個示例在其父代中創建帖子標題(除了當我搜索新的標題時它不會刪除舊標題),但是因爲它看起來超級混亂我想將班級拆分爲2. 其中一個選中按鈕,它將第一個框架擴展爲第二個框架,並在其中創建post.titles labels
。在第二幀(擴展1.幀)中, 是2個標籤,分別描述了輸入字段,2個條目和搜索按鈕。此按鈕在我的StackoverflowApi中調用advanced_search
函數,並返回一個post
對象的列表。這是我的第一堂課createPosts
功能需要的清單。如何在子類中的父類中創建Tkinter小部件
在我的第二個例子中,porblem是在第二個框架中創建的,而不是在第一個框架中創建的。 Here是一個例子,應該如何看待以及如何進行現在看起來
import tkinter as tk
import tkinter.ttk as ttk
import StackoverflowApi as API
class SearchFrame:
def __init__(self, parent, title, main_frame):
self.main_frame=main_frame
self.title=title
self.parent=parent
self.expanded = tk.IntVar()
self.expanded.set(0)
self.pagesize=tk.IntVar()
self.pagesize.set(10)
self.tags=tk.StringVar()
self.title_frame=ttk.Frame(self.parent)
self.title_frame.pack(fill="x", expand=1)
self.expand_button=ttk.Checkbutton(self.title_frame,
text=self.title +' +',
command=self.expand,
variable=self.expanded,
style='Toolbutton',
)
self.expand_button.pack(fill='x', expand=1)
#self.expand_button.configure(bg='grey') gonna fix this later
self.sub_frame=ttk.Frame(self.parent, relief='sunken', borderwidth=1)
self.tag_label=ttk.Label(self.sub_frame,
text='Tags').pack(side='left',
expand=1,
pady=5)
self.tag_entry=ttk.Entry(self.sub_frame,
textvariable=self.tags).pack(side='left',
expand=1,
pady=5)
self.pagesize_label=ttk.Label(self.sub_frame,
text='Pagesize').pack(side='left',
expand=1,
pady=5)
self.pagesize_entry=ttk.Entry(self.sub_frame,
textvariable=self.pagesize).pack(side='left',
expand=1,
pady=5)
self.search_button=ttk.Button(self.sub_frame, text='Search',
command=self.search).pack(side='left',
expand=1,
pady=5)
def search(self):
api = API.StackoverflowApi()
self.posts = api.advanced_search(tagged=list(self.tags.get()),
pagesize=self.pagesize.get())
for post in self.posts:
self.post=ttk.Label(self.parent, text=post.title).pack()
def expand(self):
if bool(self.expanded.get()):
self.sub_frame.pack(fill="x", expand=1)
self.expand_button.configure(text=self.title +' -')
else:
self.sub_frame.forget()
self.expand_button.configure(text=self.title +' +')
root = tk.Tk()
search = SearchFrame(root,'Search Options')
search.search()
root.mainloop()
這是我嘗試做兩個班分裂,因爲我認爲它看起來凌亂
from tkinter import *
from tkinter import ttk
import StackoverflowApi as API
root=Tk()
class FrameOne(ttk.Frame):
''' Main Frame in which posts should be created '''
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.pack()
#Vars
self.expanded=IntVar()
self.expanded.set(0)
self.expand_button=ttk.Checkbutton(self,
text='Seach Options +',
command=self.expand,
variable=self.expanded,
style='Toolbutton',
)
self.expand_button.pack(fill='x', expand=1)
self.sub_frame=ExtendingFrame(parent, relief='sunken', borderwidth=1)
def expand(self):#function for expanding the second frame
if bool(self.expanded.get()):
self.sub_frame.pack(fill="x", expand=1)
self.expand_button.configure(text='Seach Options -')
else:
self.sub_frame.forget()
self.expand_button.configure(text='Seach Options +')
def createPosts(self, posts):
for post in posts:
self.post=ttk.Label(self, text=post.title)
self.post.pack()
class ExtendingFrame(FrameOne):
''' this is the frame, which apperas when the checkbutton in the first frame
is clicked.
The Frame has 2 entries "tags" and "pagesize" I need these for my api call
'''
def __init__(self, parent, *args, **kwargs):
self.parent=parent
ttk.Frame.__init__(self, parent, *args, **kwargs)
#Vars
self.tags=StringVar()
self.pagesize=IntVar()
self.pagesize.set(15)
self.tag_label=ttk.Label(self, text='Tags', anchor='e')
self.tag_label.pack(fill='x', expand=1, side='left', pady=5, padx=2)
self.tag_entry=ttk.Entry(self, textvariable=self.tags)
self.tag_entry.pack(fill='x', expand=1, side='left', pady=5)
self.pagesize_label=ttk.Label(self, text='Pagesize', anchor='e')
self.pagesize_label.pack(fill='x', expand=1,side='left', pady=5, padx=2)
self.pagesize_entry=ttk.Entry(self, textvariable=self.pagesize)
self.pagesize_entry.pack(fill='x', expand=1, side='left', pady=5)
self.search_button=ttk.Button(self, text='Search',command=self.search)
self.search_button.pack(fill='x', expand=1, side='left', pady=5, padx=2)
def search(self):
''' calls the search function of the api, with the '''
api = API.StackoverflowApi()
self.posts = api.advanced_search(
tagged=list(self.tags.get()),
pagesize=self.pagesize.get()
)
#self.posts is a list of question objects returned by the api
return super().createPosts(self.posts)#I need the objects from the list in my createPosts function
main=FrameOne(root)
root.mainloop()
爲什麼你想讓小部件進入父項?如果你這樣做,那麼使用該課程有什麼意義?我認爲我從未見過一個從一個框架繼承的類的良好用例,該框架創建了自身之外的小部件。您消除了從「Frame」繼承的類的所有好處。 –
這根本不是繼承的工作方式。您不能指望一個對象創建一個繼承自身的子對象以可靠地工作。在'mainloop()'退出後,你也不能指望任何tkinter代碼工作。根據定義,它將不會退出,直到小部件全部被銷燬。 –
@BryanOakley在我真正的代碼中,第二個框架是可擴展的,所以它在我的主類中有一些我不需要的功能。第二幀也有2個條目和一個調用像'labels'這樣的函數的按鈕。我是否應該將所有這些東西都定義爲main?(我之前做過,看起來很雜亂) – MushroomMauLa