有没有一种方法可以将python tkinter中的类显示为框架,以便可以添加其他内容?

时间:2019-05-13 06:11:43

标签: python class tkinter

我想有一个tkinter窗口,同时显示天文钟和数独。 cronometer是一类,那么如何将其添加到显示数独的窗口中?

我已经设法获得了两个单独的窗口,但是我无法同时使用两个窗口。

def GUI4x4(dif):  #This function gets just called from other place

    # What I want is to be able to display this class
    # Cronometer in the main window that's created below

    class Cronometer(): 

        ...

        def __init__(self):
            self.crono=Tk()

            self.tiempo = StringVar()
            self.tiempo.set("00:00:00")

            self.label = Label(self.crono,textvariable=self.tiempo, bg="white")
            self.label.grid(column=0,row=0)
            self.label.configure(font=("Times 13 bold"))                
            self.btnI = Button(self.crono, bg="white", text="Start",command=self.iniciarT,font=("Times 11"))
            self.btnI.grid(pady=3,column=0,row=1)
            self.btnP = Button(self.crono, bg="white", text="Pause",command=self.pausarT,font=("Times 11"))
            self.btnP.grid(pady=3,column=0,row=2)
            self.btnR = Button(self.crono, bg="white", text="Restart",command=self.reiniciarT,font=("Times 11"))        
            self.btnR.grid(pady=3,column=0,row=3)


    GUI = Tk() # This creates the main window, and places
               # 34 buttons in it
    ...

    # Defining the Buttons
    btn00 = Button(GUI, text=Tablero[0][0], width=5, height=3, activebackground="lime")
    btn01 = Button(GUI, text=Tablero[0][1], width=5, height=3, activebackground="lime")
    btn02 = Button(GUI, text=Tablero[0][2], width=5, height=3, activebackground="lime")

    ...

    btn33 = Button(GUI, text=Tablero[3][3], width=5, height=3, activebackground="lime")


    #Placing the 34 buttons
    btn00.grid(row=0, column=0)
    btn01.grid(row=0, column=1)
    btn02.grid(row=0, column=2)
    ...
    btn33.grid(row=3, column=3)

1 个答案:

答案 0 :(得分:1)

使用tkinter处理此问题的标准方法是,应用程序中的每个“窗口小部件”都是基于tkinter Frame小部件的自己的类,一个用于chrono,另一个用于sudoko游戏。甚至可能有一个主要的应用程序类。

此方法的优点是,每个小部件框架都可以独立创建,然后在以后结合在一起。这些类也可以拆分为单独的代码文件。

下面是一个非常简单的示例

import tkinter as tk

class Chromometer(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.tiempo = tk.StringVar()
        self.tiempo.set("00:00:00")
        self.label = tk.Label(self,textvariable=self.tiempo, bg="white")
        self.label.grid(column=0,row=0)

class Sudoko(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.label = tk.Label(self,text="Sudoko", bg="white")
        self.label.grid(column=0,row=0)


class MainApp(tk.Frame):
    def __init__(self,master=None,**kw):
        tk.Frame.__init__(self,master=master,**kw)
        self.chrono = Chromometer(master=self)
        self.chrono.grid()
        self.sudoko = Sudoko(master=self)
        self.sudoko.grid()

if __name__ == '__main__':
    root = tk.Tk()
    app = MainApp(master=root)
    app.grid()
    root.mainloop()

每个类都有各自的方法来执行每个类所需的功能。 chromo / chrono类将具有一种更新计时器的方法。