有没有办法在pythontkinter中将类显示为一个框架,这样就可以添加其他内容?

2024-09-27 21:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想有一个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)

Tags: thetextselfguicolumnbuttonwidthgrid
1条回答
网友
1楼 · 发布于 2024-09-27 21:29:12

使用tkinter处理这个问题的标准方法是,应用程序中的每个“小部件”都是基于tkinter框架小部件的自己的类,一个类用于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类将有一个更新计时器的方法。你知道吗

相关问题 更多 >

    热门问题