在 tkinter 中的帧之间切换而不是在彼此中堆叠 [英] Switching between frames in tkinter instead of stacking in eachother

查看:31
本文介绍了在 tkinter 中的帧之间切换而不是在彼此中堆叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试本网站上有关在 tkinter 中切换窗口的所有示例,但我得到的只是将凸起框架中的按钮堆叠在顶部,而不会隐藏"其他框架

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

主屏幕:

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

产品"屏幕:

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

推荐答案

您没有使用sticky"属性来强制 ProdutoView 填充它所在的行和列.

You aren't using the "sticky" attribute to force ProdutoView to fill the row and column that it is in.

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

这篇关于在 tkinter 中的帧之间切换而不是在彼此中堆叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆