2017-11-17 111 views
0

第一個示例在其父代中創建帖子標題(除了當我搜索新的標題時它不會刪除舊標題),但是因爲它看起來超級混亂我想將班級拆分爲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() 
+1

爲什麼你想讓小部件進入父項?如果你這樣做,那麼使用該課程有什麼意義?我認爲我從未見過一個從一個框架繼承的類的良好用例,該框架創建了自身之外的小部件。您消除了從「Frame」繼承的類的所有好處。 –

+1

這根本不是繼承的工作方式。您不能指望一個對象創建一個繼承自身的子對象以可靠地工作。在'mainloop()'退出後,你也不能指望任何tkinter代碼工作。根據定義,它將不會退出,直到小部件全部被銷燬。 –

+0

@BryanOakley在我真正的代碼中,第二個框架是可擴展的,所以它在我的主類中有一些我不需要的功能。第二幀也有2個條目和一個調用像'labels'這樣的函數的按鈕。我是否應該將所有這些東西都定義爲main?(我之前做過,看起來很雜亂) – MushroomMauLa

回答

0

上面的代碼下面的代碼有兩個類,MainApp來構造我們的應用程序的最外層框架,然後SearchFrame類使用MainApp的方法在其父窗口小部件中創建一個標籤。在這種情況下SearchFrame的父部件是MainApp。如果您想要看到標籤實際上是在父控件子類MainApp內創建的,則可以將幾何管理器註釋到self.frame2。再次,我懷疑這是不錯的做法:

import tkinter as tk 

root = tk.Tk() 

class MainApp(tk.Frame): 
    def __init__(self, master): 
     super().__init__(master) 

     #a child frame of MainApp object 
     self.frame1 = tk.Frame(self) 

     tk.Label(self.frame1, text="This is MainApp frame1").pack() 

     self.frame1.grid(row=0, column=0, sticky="nsew") 


     #another child frame of MainApp object 
     self.frame2 = SearchFrame(self) 

     self.frame2.grid(row=0, column=1, sticky="nsew") 




    def create_labels(self, master): 
     return tk.Label(master, text="asd") 


class SearchFrame(tk.Frame): 
    def __init__(self, master): 
     super().__init__(master) 

     self.label = tk.Label(self, text="this is SearchFrame") 
     self.label.pack() 

     master.label1 = MainApp.create_labels(self, master) 

     master.label1.grid() 


mainAppObject = MainApp(root) 
mainAppObject.pack() 

root.mainloop() 
相關問題