顶级类的新实例使小部件重叠

顶级类的新实例使小部件重叠

本文介绍了顶级类的新实例使小部件重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常是python和tkinter的新手.我已经进行了大约一年的编程,并且我刚刚开始尝试将每个tkinter顶级窗口设为自己的类,因为我听说这是正确的方法.

I'm generally new to python and tkinter. I've been programming maybe about a year or so, and I've just started to try to make each tkinter toplevel window its own class because I've heard that it's the right way to do it.

我正在制作一个程序,其中有一个带有按钮的树状视图,可向其中添加内容.该按钮将打开一个新窗口,允许用户输入内容.我遇到的问题是,当我必须实例化第一个窗口以更新树视图时,似乎正在使第一个窗口上的所有小部件加倍.这导致它不断积累,看起来很奇怪.

I'm making a program in which I have a treeview with a button to add contents to it. The button opens up a new window that allows the user to input the contents. The problem I'm having is when I have to instantiate the first window to update the treeview, it seems like it's doubling all of the widgets on the first window. This causes it to keep building up and it looks strange.

这是正常现象还是有更好的方法呢?

Is this normal or is there a better way to do it?

谢谢.我可以在需要时发布图片或代码.

Thank you. I can post a picture or my code if necessary.

缩短代码

from tkinter import *
from tkinter import ttk


class MainWindow:
    Items = {'test': ['Material', '500']}
    def __init__(self, master):
        self.master = master

        self.style = ttk.Style()
        self.style.configure('TLabel', font=12)

        ttk.Label(self.master, text="Items").grid(row=0, column=0, columnspan=3)

        self.frmItems = ttk.Frame(self.master)
        self.frmItems.grid(row=1, column=0, padx=5, pady=5, columnspan=3)

        self.treeItems = ttk.Treeview(self.frmItems, columns=(0, 1, 2))
        self.treeItems.column('#0', width=0, minwidth=0)
        self.treeItems.column(1, width=80)
        self.treeItems.column(2, width=80)
        self.treeItems.heading(0, text="Name")
        self.treeItems.heading(1, text="Type")
        self.treeItems.heading(2, text="Price")
        self.treeItems.grid(row=0, column=0)

        self.itemscroll = ttk.Scrollbar(self.frmItems, command=self.treeItems.yview)
        self.itemscroll.grid(row=0, column=1, sticky='ns')
        self.treeItems.config(yscrollcommand=self.itemscroll.set)

        ttk.Button(self.master, text="New", command=self.item_input_show).grid(row=2, column=0, padx=5, pady=5,
                                                                              sticky='e')
        ttk.Button(self.master, text="Edit").grid(row=2, column=1, padx=5, pady=5)
        ttk.Button(self.master, text="Remove").grid(row=2, column=2, padx=5, pady=5, sticky='w')

    def item_input_show(self):
        ItemInput(self.master)


class ItemInput:
    def __init__(self, master):
        self.master = master
        self.MainWindow = MainWindow(master)

        self.topItemInput = Toplevel(self.master)
        self.topItemInput.title("Input Item Properties")


def main():
    root = Tk()
    MainWindow(root)
    root.mainloop()

if __name__ == "__main__":
    main()

推荐答案

每次按下New按钮,您都在呼叫class MainWindow:.这将一遍又一遍地重新制作所有小部件.他们创建MainWindow的方式会影响您与MainWindow进行交互的方式.

You are calling class MainWindow: every time you press the New Button. This is remaking all the widgets over and over. And they way you are creating the MainWindow is affecting how you can interact with MainWindow.

更改:

def main():
    root = Tk()
    MainWindow(root)
    root.mainloop()

if __name__ == "__main__":
    main()

收件人:

 if __name__ == "__main__":
    root = Tk()
    main = MainWindow(root)
    root.mainloop()

进行此更改后,便可以与main

Once you have made this change you are able to interact with the instance attributes and methods of main

下面是代码的修改版本.您会注意到,当您按下我添加到TopLevel窗口的按钮时,它将打印来自main变量的属性和方法的信息.还将在MainWindow的输入框中放置一些文本.

Below is a modified version of your code. You will notice when you press the button I added to the TopLevel window it will print information from an atribute and method of main variable. It will also place some text in the entry box in the MainWindow.

from tkinter import *
from tkinter import ttk

class MainWindow:
    def __init__(self, master):
        self.master = master
        self.btn = ttk.Button(self.master, text="New", command=self.item_input_show)
        self.btn.pack(side = TOP)
        self.entry = Entry(self.master)
        self.entry.pack(side = BOTTOM)
        self.numbers = 200

    def two_plus_x(self, x):
        math = 2 + x
        return math

    def item_input_show(self):
        ItemInput(self.master)


class ItemInput:
    def __init__(self, master):
        self.master = master
        self.topItemInput = Toplevel(master)
        self.btn = ttk.Button(self.topItemInput, text="Use method in MainWindow", command = self.do_something_from_main)
        self.btn.pack()

    def do_something_from_main(self):
        print(main.numbers)
        print(main.two_plus_x(10))
        main.entry.delete(0, END)
        main.entry.insert(0, "From ItemInput Class")

# notice I removed def main(): as it was preventing us from interacting with the main variable.
if __name__ == "__main__":
    root = Tk()
    main = MainWindow(root)
    root.mainloop()

这篇关于顶级类的新实例使小部件重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:44