python - 关闭并打开另一个窗口

标签 python python-3.x tkinter


我正在尝试编写一个脚本,该脚本会用英语询问一个单词,然后显示其含义。我可以提出问题,但没有显示答案窗口。我到目前为止编写的代码如下。在第二个窗口中,它就像一个新页面一样开始。我该如何修改它?现在,它显示标签,但按钮不显示。

from tkinter import *


class Application(Frame):
    def __init__(self, master):
        """Initialize the Frame"""
        Frame.__init__(self, master)
        self.grid()
        self.button_clicks = 0  # count the number of button clicks
        self.create_widgets()

    def root_close(self):
        global root
        root.destroy()
        self.button_clicky()

    def create_widgets(self):
        """Button displays number of clicks"""
        if clicker % 2 == 0:
            self.soru = Label(self, text="Kelime: " + kelime)
            self.soru.grid(row=0, column=0, columnspan=2, sticky=W)

            self.btn_submit = Button(self, text="Submit", command=self.root_close)
            self.btn_submit.grid(row=3, column=1, sticky=W)
        else:
            self.cevap = Label(self, text="Kelimenin türkçe anlamları:\n" + anlam)
            self.cevap.grid(row=0, column=0, columnspan=2, sticky=W)

            self.btn_okay = Button(self, text="Bildim", command=self.dogru)
            self.btn_submit.grid(row=3, column=0, sticky=W)

            self.btn_okay = Button(self, text="Bilemedim", command=self.yanlis)
            self.btn_submit.grid(row=3, column=2, sticky=W)

    def button_clicky(self):
        global clicker
        clicker += 1

    def dogru(self):
        #will do stuff
        self.root_close()

    def yanlis(self):
        self.root_close()


clicker = 0
kelime = "apple"
anlam = "elma"
root = Tk()
root.title("Ask word")
root.geometry("200x85")
app = Application(root)

root.mainloop()

最佳答案

所以我从你的问题中得到的是,你有第一个带有 Kelime 问题的窗口,并且你想在单击后在 create_widgets() 函数中使用 else 子句打开另一个窗口提交按钮。这里的问题是,当您运行 root_close() 时,您实际上终止了整个程序(程序运行是因为根位于由 root.mainLoop()< 创建的循环上)/)。如果您想在关闭另一个窗口时打开一个窗口,请查看Closing current window when opening another window .

关于python - 关闭并打开另一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767496/

相关文章:

python - 如何创建返回列表包含字符串的类型提示?

python - 如何将 python 脚本通过管道传递给命令 (boost::program::options)

Python:无法使用 beautifulsoup 获得任何输出

python - tkinter GUI 写入文本小部件并替换文本

python - 试图在按住 tkinter 按钮时保持功能持续运行

python - 重写内置类型方法

python - 我的 __str__ 太长了,最好的写法

python - 自定义 GUI 元素的类未显示

Python:当函数在后台运行时,是否可以创建一个具有动态字符串的 tkinter 标签?

python - 在线程和rest-api的情况下,mqtt客户端不会收到消息