如何调用窗口小部件类框架并将其打包到主窗口应用程序类中? Tkinter的

时间:2013-12-11 13:13:29

标签: python class python-2.7 user-interface tkinter

我在创建数据库GUI时遇到问题。 我正在创建类 * (tk.Frame)的小部件: 然后我尝试在“MainApplication(tk.Frame):”类中创建并打包它们。 但是,小部件不会出现。 我对根目录和“自我”有点困惑,我将其传递到MainApplication,然后从那里进入widget类。 如何显示小部件?

'''
Created on 26/11/2013

@author: User
'''
# ============== Import ==========================================#
import Tkinter as tk
import MySQLdb
import tkFont
''' '''  
# ============== Functions & Helpers =========================== #
def new_customer_window():
    top = tk.Toplevel()
    top.title("new customer box")

    e1 = tk.Entry(top)
    e2 = tk.Entry(top)

    e1.grid(row=0, column=1)
    e2.grid(row=1, column=1)

def launchNext():
    pass

def updateRecordStatus(table, rowID, newstatus):
    top = tk.Toplevel()
    top.title("Update called")
    cmd = "UPDATE jobs SET STATUS=%s WHERE ID=%s"  # CHANGE jobs to VARIABLE!!
    print(table, rowID, newstatus)
    db = MySQLdb.connect ("127.0.0.1", "root", "", "popupbikes")
    cursor = db.cursor()
    cursor.execute(cmd, (newstatus, rowID))
    db.close()

def editRecord(table, rowID):
    pass

class data:
    def __init__(self, dataName):
        db = MySQLdb.connect ("127.0.0.1", "root", "", "popupbikes")
        self.cursor = db.cursor()
        self.dataName = dataName
        self.cursor = db.cursor()
        s = "Select * from %s" % dataName
        self.cursor.execute(s)
        dataAll = self.cursor.fetchall()
        db.close()
    def row_count(self):    
        self.numrows = self.cursor.rowcount()
        return self.numrows

    def fields(self):
        self.fields = len(self.cursor.description())
        return self.fields
''' '''  
# ============== GUI Classes ===========================#
class JobTab(tk.Frame):
    def __init__(self, master, windowName, WIDTH, HEIGHT):
        tk.Frame.__init__(self, master)
        self.master = master


# THIS IS THE WIDGET I AM TRYING TO DRAW ON THE MAIN WINDOW AS A TEST
class ActionButtons(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)
        self.master = master

        self.TitleFont = tkFont.Font(family="Harlow Solid Italic", size=30)

        # Create Buttons
        self.actionButtonContainer = tk.Frame(self.master)  # Frame for set
        self.newJobButton = tk.Button(self.actionButtonContainer,
                                      text="New Job",
                                      command=new_customer_window,
                                      font=self.TitleFont)
        self.editCustomerButton = tk.Button(self.actionButtonContainer,
                                            text="Edit Customer Details",
                                            command=new_customer_window,
                                            font=self.TitleFont)
        # Pack Frame
        self.newJobButton.pack(expand=1, padx=40, pady=20)
        self.editCustomerButton.pack(expand=1, padx=40, pady=20)
        print("Buttons Packed??")

class DataWidget(tk.Frame):
    # Rowbuttons specified as a list with [[label, command function]i]
    def __init__(self, master, datatype, fields, rows, rowbuttons):
        tk.Frame.__init__(self, master)
        self.master = master
        self.dataype = datatype
        self.fields = fields
        self.rows = rows
        self.rowbuttons = rowbuttons
        self.container = 1
''' '''     
# ============== Build GUI in root ===========================#
class MainApplication(tk.Frame):
    def __init__(self, master, windowName, WIDTH, HEIGHT):
        # Root window set-up
        tk.Frame.__init__(self, master)
        self.TitleFont = tkFont.Font(family="Harlow Solid Italic", size=30)
        self.master = master # Self assign root for passing into other classes        
        self.master.title(windowName)   # Title
        w, h = master.winfo_screenwidth()-15, master.winfo_screenheight()-65
        master.geometry("%dx%d+0+0" % (w, h))   # Root Window Geometry
        # Create widget objects
        self.labelTitle = tk.Label(master, text=windowName, 
                                   font=self.TitleFont, bg="white", width =55)
        self.actionbuttons = ActionButtons(self)  # THIS IS MAYBE WHERE IT IS
                                                  # GOING WRONG!?
        # Pack everything into the root window!
        self.labelTitle.pack()  # THIS PACKS PROPERLY
        self.actionbuttons.pack()   # THIS DOESN"T APPEAR
''' '''     
# ============== Initialize Program ===========================#
def main(): 
    root = tk.Tk()
    # Set fonts 
    SubTitleFont = tkFont.Font(family="Cambria", size=13)
    HeaderFont = tkFont.Font(family="Cambria", size=10, weight="bold") 
    TextFont = tkFont.Font(family="Cambria", size=10)

    # Initialize Root Window
    SCREEN_WIDTH = root.winfo_screenwidth() - 30
    SCREEN_HEIGHT = root.winfo_screenheight() - 100
    PopUpApp = MainApplication(root, "Pop-Up Bikes Home",
                               SCREEN_WIDTH, SCREEN_HEIGHT)
    PopUpApp.pack(side="top", fill="both", expand=True)
    root.mainloop()

if __name__ == '__main__':
    main()

谢谢!

1 个答案:

答案 0 :(得分:1)

看起来您正在创建一个名为actionButtonContainer的框架,并在其中放置按钮。但是,您永远不会使用包或网格来使此框架可见,因此它及其所有子项将是不可见的。

由于ActionButtons已经是一个框架,我不明白为什么你需要这个容器框架。您可能只想让按钮成为self的子项。