问题:创建一个类似向导的程序,向用户显示一系列窗口,用户可以在窗口之间单击下一步和后退按钮.
解决方案是:
>创建一个根窗口.
>创建与要呈现给用户的窗口一样多的帧.将所有帧附加到根窗口.
>使用所需的所有小部件填充每个框架.
>当已填充所有帧时,使用grID_forget()方法隐藏每个帧,但不隐藏第一帧以使其成为可见帧.框架上的所有子窗口小部件都将隐藏在框架中.
>当用户单击窗口上的“下一个”或“后退”按钮时,调用隐藏其他帧的子例程(使用grID_forget())并使所需的帧可见(使用grID()).
>如果希望程序结束,请对根窗口使用destroy – 方法.
因此,您将创建一个窗口并在其上显示不同的框架.
(顺便说一句,开始学习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 tkinterimport tkinter.ttkdef 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,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,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,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,sticky=(tkinter.N)) second_window_next_button = tkinter.button(second_frame,command = call_third_frame_on_top) second_window_next_button.grID(column=1,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,sticky=(tkinter.N)) # Create the button for the frame third_window_back_button = tkinter.button(third_frame,command = call_second_frame_on_top) third_window_back_button.grID(column=0,sticky=(tkinter.N)) third_window_quit_button = tkinter.button(third_frame,command = quit_program) third_window_quit_button.grID(column=1,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,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,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,tkinter.E))def quit_program(): root_window.destroy()################################ Main program starts here :) ################################# Create the root GUI window.root_window = tkinter.Tk()# define window sizewindow_wIDth = 200window_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'] = 2first_frame['relIEf'] = 'sunken'first_frame.grID(column=0,tkinter.E))second_frame=tkinter.ttk.Frame(root_window,height=window_heigth)second_frame['borderwIDth'] = 2second_frame['relIEf'] = 'sunken'second_frame.grID(column=0,tkinter.E))third_frame=tkinter.ttk.Frame(root_window,height=window_heigth)third_frame['borderwIDth'] = 2third_frame['relIEf'] = 'sunken'third_frame.grID(column=0,tkinter.E))# Create all Widgets to all framescreate_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 - looproot_window.mainloop()解决方法 因为你冒昧地把答案作为一个问题发表.我想发表评论作为答案,并建议您可以将此贡献给TkDocs(点击他们的 About tab,他们谈论对该网站的贡献).
我认为如果用更多的例子来改进这个网站比将这个网站变成一本食谱更好.我认为你也可以为Active State recipes做出贡献,而且他们似乎是Tcl / Tk火炬的载体,所以Tkinter的东西也很有意义.
总结以上是内存溢出为你收集整理的导航 – 解决方案:Python3 Tkinter使用后退和下一个按钮从一个窗口跳转到另一个窗口全部内容,希望文章能够帮你解决导航 – 解决方案:Python3 Tkinter使用后退和下一个按钮从一个窗口跳转到另一个窗口所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)