【问题标题】:python 3.8 tkinter calling new window from method creates a blank windowpython 3.8 tkinter从方法调用新窗口创建一个空白窗口
【发布时间】:2021-08-18 17:00:35
【问题描述】:

我试图通过按下现有窗口的按钮来调用新窗口。创建新窗口时,原始窗口应关闭。当我按下按钮时,新窗口将按预期显示,但还会出现一个空白窗口。使用tk.Tk()tk.Toplevel() 将导致相同的结果。

稍后在程序中我想再次销毁创建的窗口。当使用tk.Tk() 时,当destroy() 方法被应用到新窗口时,用鼠标关闭空白窗口会产生错误“应用程序已被销毁”。

import tkinter as tk
from tkinter import messagebox


def main():

    root = tk.Tk()
    root.title("Hauptmenü")

    Menue = MainMenue(root)
    Menue.pack()

    root.mainloop()


class MainMenue(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.button_rennen = tk.Button(self, text="New Window", width=20, command=self.call_bet)
        self.button_rennen.pack()

    def call_bet(self):
        self.destroy()
        root2 = tk.Tk()
        Bet = BetFrame(root2)
        Bet.pack()



class BetFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.button = tk.Button(text="Wette platzieren",
                           command=self.question)

        self.button.pack()


    def question(self):
        dialog = tk.messagebox.askokcancel(message="Destroy Window?")

        if dialog is True:
            self.destroy()


main()

我正在为每个新窗口创建一个新类,因为原始程序应该返回一些变量。 我知道这个话题已经有很多问题,但对我来说,这些问题似乎都不太适合并帮助我找到解决问题的方法。我很感激任何帮助!

【问题讨论】:

  • self.destroy(); root2 = tk.Tk(); Bet = BetFrame(root2); Bet.pack()代替Bet = BetFrame(self.master); Bet.pack(); self.destroy()
  • 谢谢,这解决了空白窗口的问题。现在question() 方法中的destroy() 函数没有关闭任何东西。我该如何解决?
  • BetFrame 中创建按钮时需要传递self。我会写一个正确的答案

标签: python tkinter new-window createwindow


【解决方案1】:

看看这个:

import tkinter as tk
from tkinter import messagebox


class MainMenue(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        self.button_rennen = tk.Button(self, text="New Window", width=20,
                                       command=self.call_bet)
        self.button_rennen.pack()

    def call_bet(self):
        # `self.master` is the window
        Bet = BetFrame(self.master)
        Bet.pack()
        self.destroy()


class BetFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)

        # You need to tell the button that its master should be this BetFrame
        # If you don't it will assume you mean the window so
        # `self.destroy()` isn't going to destroy the button.
        self.button = tk.Button(self, text="Wette platzieren", command=self.question)
        self.button.pack()

    def question(self):
        dialog = tk.messagebox.askokcancel(message="Destroy Window?")

        if dialog is True:
            self.destroy()

def main():
    root = tk.Tk()

    Menue = MainMenue(root)
    Menue.pack()

    root.mainloop()


main()

您的代码很棒,但有 2 个错误:

  • 与其创建新窗口,不如重用旧窗口。
  • 当您在BetFrame 类中创建按钮时,您不会为主参数(第一个参数)传递任何内容。这就是为什么当您使用 self.destroy() 销毁 BetFrame 对象时按钮不会被销毁的原因

【讨论】:

  • 非常感谢。这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多