在 tkinter 窗口之间来回切换 [英] Go back and forth between tkinter windows

查看:102
本文介绍了在 tkinter 窗口之间来回切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力创建一个包含多个 Windows 的 tkinter 项目.我想要的只是定期执行我的程序并逐步倒退.经过大量研究,我正在努力寻找一个示例来解释如何做到这一点.

I am currently struggling to create a tkinter Project which includes several Windows. All I want is to go forward regularly through my program and backwards step by step. After a lot of research I am struggling to find an example explaining how to do it.

我尝试在几个帖子之后创建一个最小的示例,但它还没有工作,而且老实说,我真的不明白我在做什么.

I tried to create a minimal example following several posts but it isn't working yet and I don't really understand what I am doing to be honest.

我最大的参考是这里的这篇文章,但它很长而且很复杂:tkinter - 使用按钮在帧之间来回切换

My biggest reference is this post here but it's quite long and complex: tkinter - Going Back and Forth Between Frames Using Buttons

另一方面,这里的这个例子对我来说还不够.结合和解释可能已经成功了:Tkinter 打开和关闭顶层窗口

This example here on the other hand isn't quite enough for me to get it. Both combined and explained might do the trick already: Tkinter open and close Toplevel windows

  • 你能帮我创建一个最小的例子吗(有或没有实际窗口的代码)?
  • 并解释发生了什么.我认为这很简单,所以我猜解释可能会很短.(如果你知道一个教程网站解释得很好,我也会很高兴)

提前致谢,希望我们能为我和其他人创建有用的资源.

Thanks in advance, hopefully we can create a helpful resource for me and others.

 def Forward(self):    
        # Open secondary Window
        Secondary_Win = Toplevel()  
        #Close primary Window
        Main_Win.withdraw() #.deiconify() to show again
        Main_Win.destroy()

 def Backward(self):    
        # Close secondary Window
        Secondary_Win.withdraw 
        #Open primary Window
        Main_Win.deiconify()

正如@stovfl 指出的那样:我想在实际的 Windows 之间切换,而不仅仅是在框架之间切换.抱歉,措辞打嗝.

as @stovfl pointed out to clarify: I want to switch between actual Windows not just framees. Sorry for the wording hiccup.

推荐答案

也许这与您要寻找的内容有些接近:

Perhaps this is somewhat close to what you're looking for:

from tkinter import *


root = Tk()



class temp_frame:

    def __init__(self, master):
        self.master = master
        self.secondary_win = None
        self.btn_next = Button(self.master, text="Forward", command=self.Forward)
        self.btn_next.pack()


    def Forward(self):    
        # Open secondary Window
        if not self.secondary_win:
            self.secondary_win = Toplevel()
            back_btn = Button(self.secondary_win, text="Back", command=self.Backward)
            back_btn.pack()
            self.master.withdraw()
        else:
            self.secondary_win.deiconify()
            self.master.withdraw()



    def Backward(self):    
        self.secondary_win.withdraw()
        self.master.deiconify()


temp = temp_frame(root)

root.mainloop()

说明:

一个框架是在 temp_frame 类的帮助下创建的.该框架包含向后和向前的功能,按下前进时打开一个新窗口,按下后退时退出新窗口.新窗口退出时,主窗口前移.

A frame is created with the help of the class temp_frame. The frame holds the functions for backwards and forwards, opening a new window when forward is pressed and withdrawing the new window when backwards is pressed. When the new window is withdrawn, the main window is brought forward.

修改代码以避免每次按下前进"时都创建一个新窗口.

Revised code to avoid creating a new window everytime "Forward" was pressed.

这篇关于在 tkinter 窗口之间来回切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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