2013-12-11 73 views
0

我在創建數據庫GUI時遇到了麻煩。 我創建的小部件如類*(tk.Frame): 後來我嘗試和創造,並在「MainApplication(tk.Frame)收拾他們:類 然而,小部件不會出現 我。約根,「自我」是我傳遞到MainApplication,然後從那裏進入窗口小部件類有點困惑。 如何獲得小部件顯示出來?如何調用窗口小部件類框架並將其打包到主窗口應用程序類中? tkinter

''' 
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() 

謝謝!

+0

任何其他提示也將不勝感激! – Pete

+0

你好,彼得!歡迎來到SO。作爲一般性建議:儘量避免一般和開放式問題。你根本得不到真正的答案或答案。保持簡潔,實用並達到某一點 - 一個*單*點 - 通過一些示例和回溯來獲得它。這樣你的答案可以在5分鐘內得到。 –

回答

1

看起來您正在創建一個名爲actionButtonContainer的框架,並在其中放置按鈕。但是,您使用包或網格使這個框架可見,所以它和它的所有孩子都將隱形。

由於ActionButtons已經是一個框架,我不明白你爲什麼需要這個容器框架。您可能只想製作self的按鈕子項。

+1

啊,布賴恩奧克利再次拯救!你解決了它!我基本上是通過你的文章學習python。非常感謝! – Pete

相關問題