简体   繁体   English

在tkinter中切换帧,而不是彼此堆叠

[英]Switching between frames in tkinter instead of stacking in eachother

I have been trying all the many examples in this website about switching windows in tkinter, but all I get is the buttons from the raised frame to be stacked on top, without "hiding" the other frames 我一直在尝试使用该网站中有关在tkinter中切换窗口的所有示例,但是我得到的只是凸起框架中的按钮要堆叠在顶部,而不会“隐藏”其他框架

Main screen : 主屏幕 :

import tkinter as tk
import Controller.ProdutoController as viewProduto

class Main:
    def __start__(self):

        self.roottk = tk.Tk()
        self.root = tk.Frame(self.roottk)

        self.root.pack(side="top", fill="both", expand=True)
        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=1)
        self.viewProduto = viewProduto.ProdutoController(self, self.root)
        self.viewMain = MainView(self, self.root)

        self.viewMain.tkraise()
        self.viewMain.master.master.title("Tela Principal")
        self.roottk.mainloop()



    def toprodutos(self):

        self.viewProduto.viewProduto.tkraise()

    def tomain(self):

        self.viewMain.tkraise()


class MainView(tk.Frame):

    def __init__(self, ct, root):
        tk.Frame.__init__(self,root)
        self.startUI()
        self.ct = ct
        self.grid( row = 0 , column = 0, sticky = "nsew")



    def startUI(self):

        botaoProdutos = tk.Button(self, text = "Produtos", command = self.toprodutos , padx = 5 , pady = 5)
        botaoProdutos.pack(side = "top")

        botaoEntrada = tk.Button(self, text="Entrada", command=self.toentrada, padx=5, pady=5)
        botaoEntrada.pack(side="top")

        botaoSaida = tk.Button(self, text="Saída", command=self.tosaida, padx=5, pady=5)
        botaoSaida.pack(side="top")

    def toprodutos(self):
        self.ct.toprodutos()
    def toentrada(self):
        return
    def tosaida(self):
        return

"Produtos" screen : “产品”屏幕:

class ProdutoController:

    def __init__(self, viewMain, root):

        self.produtos = []
        self.viewMain = viewMain
        self.viewProduto = ProdutoView(root, self)


    def newproduto(self, nome, preco, quantidade):
        return

    def listprodutos(self):
        return

class ProdutoView(tk.Frame):
    def __init__(self, root, ct):
        tk.Frame.__init__(self, root)
        self.createWidgets()
        self.ct = ct
        self.grid(row = 0, column = 0)


    def createWidgets(self):

        self.list = tk.Button(self)
        self.list["text"] = "List"
        self.list["command"] = self.listProdutos
        self.list.pack(side="top")

    def listProdutos(self):
        self.ct.listprodutos()

You aren't using the "sticky" attribute to force ProdutoView to fill the row and column that it is in. 您没有使用“ sticky”属性来强制ProdutoView填充ProdutoView在的行和列。

class ProdutoView(tk.Frame):
    def __init__(self, root, ct):
        ...
        self.grid(row = 0, column = 0, sticky="nsew")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM