解决方案:Python3的Tkinter从一个窗口跳到另一个背部和下一个按钮(Solution: P

2019-07-30 01:18发布

我一直在学习的Tkinter在python3,并发现它很难在网上找到良好的文档和解答。 为了帮助别人,我决定发布一个简单的问题的解决方案,有似乎是没有在线文档同样的问题挣扎。

问题:创建一个精灵般的程序中,向用户呈现一系列窗口,用户可以在Windows一下旁边和背部之间移动 - 按钮。

解决的办法是:

  • 创建一个根窗口。
  • 你有窗口呈现给用户创造尽可能多帧。 附上所有帧到根窗口。
  • 填充它所需的所有部件每帧。
  • 当所有的帧已被填充,隐藏与各帧grid_forget()方法,但将第一帧未隐藏,使得它成为可见的。 在框架上的所有子控件将与框架被隐藏。
  • 当用户点击下一步或后退按钮的窗口上,调用隐藏其它帧(具有子程序grid_forget()并使所需要的可见一个(与grid()
  • 当您希望程序结束,使用破坏 - 根窗口方法。

所以,你将创建一个窗口,并显示在它不同的帧。

(顺便说一下,最好的地方开始学习Tkinter的是: http://www.tkdocs.com/tutorial/index.html )

这里是一个Python3示例实现。 它有3个简单的窗口,每一个文本标签和两个按钮通过不同的窗口导航。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#

# Creates three "windows" that the user can navigate through using Back and Next - buttons.

import tkinter
import tkinter.ttk

def create_widgets_in_first_frame():
    # Create the label for the frame
    first_window_label = tkinter.ttk.Label(first_frame, text='Window 1')
    first_window_label.grid(column=0, row=0, pady=10, padx=10, sticky=(tkinter.N))

    # Create the button for the frame
    first_window_quit_button = tkinter.Button(first_frame, text = "Quit", command = quit_program)
    first_window_quit_button.grid(column=0, row=1, pady=10, sticky=(tkinter.N))
    first_window_next_button = tkinter.Button(first_frame, text = "Next", command = call_second_frame_on_top)
    first_window_next_button.grid(column=1, row=1, pady=10, sticky=(tkinter.N))

def create_widgets_in_second_frame():
    # Create the label for the frame
    second_window_label = tkinter.ttk.Label(second_frame, text='Window 2')
    second_window_label.grid(column=0, row=0, pady=10, padx=10, sticky=(tkinter.N))

    # Create the button for the frame
    second_window_back_button = tkinter.Button(second_frame, text = "Back", command = call_first_frame_on_top)
    second_window_back_button.grid(column=0, row=1, pady=10, sticky=(tkinter.N))
    second_window_next_button = tkinter.Button(second_frame, text = "Next", command = call_third_frame_on_top)
    second_window_next_button.grid(column=1, row=1, pady=10, sticky=(tkinter.N))

def create_widgets_in_third_frame():
    # Create the label for the frame
    third_window_label = tkinter.ttk.Label(third_frame, text='Window 3')
    third_window_label.grid(column=0, row=0, pady=10, padx=10, sticky=(tkinter.N))

    # Create the button for the frame
    third_window_back_button = tkinter.Button(third_frame, text = "Back", command = call_second_frame_on_top)
    third_window_back_button.grid(column=0, row=1, pady=10, sticky=(tkinter.N))
    third_window_quit_button = tkinter.Button(third_frame, text = "Quit", command = quit_program)
    third_window_quit_button.grid(column=1, row=1, pady=10, sticky=(tkinter.N))

def call_first_frame_on_top():
    # This function can be called only from the second window.
    # Hide the second window and show the first window.
    second_frame.grid_forget()
    first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

def call_second_frame_on_top():
    # This function can be called from the first and third windows.
    # Hide the first and third windows and show the second window.
    first_frame.grid_forget()
    third_frame.grid_forget()
    second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

def call_third_frame_on_top():
    # This function can only be called from the second window.
    # Hide the second window and show the third window.
    second_frame.grid_forget()
    third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

def quit_program():
    root_window.destroy()

###############################
# Main program starts here :) #
###############################

# Create the root GUI window.
root_window = tkinter.Tk()

# Define window size
window_width = 200
window_heigth = 100

# Create frames inside the root window to hold other GUI elements. All frames must be created in the main program, otherwise they are not accessible in functions. 
first_frame=tkinter.ttk.Frame(root_window, width=window_width, height=window_heigth)
first_frame['borderwidth'] = 2
first_frame['relief'] = 'sunken'
first_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

second_frame=tkinter.ttk.Frame(root_window, width=window_width, height=window_heigth)
second_frame['borderwidth'] = 2
second_frame['relief'] = 'sunken'
second_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

third_frame=tkinter.ttk.Frame(root_window, width=window_width, height=window_heigth)
third_frame['borderwidth'] = 2
third_frame['relief'] = 'sunken'
third_frame.grid(column=0, row=0, padx=20, pady=5, sticky=(tkinter.W, tkinter.N, tkinter.E))

# Create all widgets to all frames
create_widgets_in_third_frame()
create_widgets_in_second_frame()
create_widgets_in_first_frame()

# Hide all frames in reverse order, but leave first frame visible (unhidden).
third_frame.grid_forget()
second_frame.grid_forget()

# Start tkinter event - loop
root_window.mainloop()

Answer 1:

正如你已经采取了自由张贴的答案的问题。 我想发布作为回答的评论和建议,也许你应该有助于这TkDocs(点击他们关于标签和他们谈促进网站)。

我觉得这是更好,如果该网站是改进比把这个网站变成了一本食谱更多的例子。 我想你也可以向活动状态的食谱 ,他们似乎是火炬的Tcl / Tk的运营商,所以Tkinter的东西,使一个很大的意义也有。



Answer 2:

感谢您的工作 - 我用它作为灵感在这个例子中,虽然在内容方面非常轻,是让你能之间切换窗口的任意数量的一个很酷的方式。 你可以移动下一个和后退按钮的位置,把它们变成箭头,任何你想要的。

from tkinter import *
master=Tk()

class makeframe(object):
    def __init__(self,i):
        self.i=i
        self.frame=Frame(master)
        self.nextbutton=Button(self.frame,text='next',command=self.next)
        self.nextbutton.grid(column=2,row=0)
        self.backbutton=Button(self.frame,text='back',command=self.back)
        self.backbutton.grid(column=0,row=0)
        self.label=Label(self.frame,text='%i'%(self.i+1)).grid(column=1,row=0)
    def next(self):
        self.frame.grid_forget()
        p[self.i+1].frame.grid()
    def back(self):
        self.frame.grid_forget()
        p[self.i-1].frame.grid()

n=7
p=[0]*n
for i in range(n):
    p[i]=makeframe(i)
p[0].frame.grid()
p[0].backbutton.config(state=DISABLED)
p[-1].nextbutton.config(state=DISABLED)


文章来源: Solution: Python3 Tkinter Jump from one window to another with back and next buttons