简体   繁体   English

在tkinter中的帧之间切换

[英]Switching between frames in tkinter

Although I know that people have asked this question before, I have tried the methods on the posts and not succeeded. 尽管我知道人们以前曾问过这个问题,但我已经尝试过帖子中的方法,但没有成功。 I am trying to switch between 2 frames on the click of a button. 我试图单击一个按钮在2帧之间切换。 Here is my code so far: 到目前为止,这是我的代码:

from tkinter import *
window  = Tk()


nframe = Frame(window,width = 100,height = 100)
nframe.pack()
conjframe  = Frame(window,width = 100,height  = 100)
transframe = Frame(window,width = 100,height = 100)

window.geometry("100x100")


def raisenframe():
    nframe.tkraise()

def raiseconjframe():
    conjframe.tkraise()

def raisetransframe():
    transframe.tkraise()

def conj():
    print("this is a conjugator")
    conjframe.tkraise()
def trans():
    print("this is a translator")
    transframe.tkraise()
    transframe.pack()

Label(conjframe,text = 'hola').pack()
conjugator = Button(nframe, text="Conjugator", command=lambda:raiseconjframe)
conjugator.pack()

translator = Button(nframe, text="Translator", command=lambda:raisetransframe)
translator.pack()

raisenframe()
window.mainloop()

The problem is that when I click the button, it doesn't seem to be switching to any of the other frames although I think I have done everything correctly. 问题是,当我单击该按钮时,尽管我认为我已正确完成所有操作,但似乎并没有切换到其他任何框架。 Could anyone help me? 有人可以帮我吗?

The essence of getting a layout with stacked frames is to have Frames located on top of one another. 获得具有堆叠框架的布局的本质是使框架彼此重叠。 You can achieve this by gridding frames on the same location. 您可以通过在相同位置将框架网格化来实现。 To observe the raise operation you need to place a widget in each frame. 要观察提升操作,您需要在每个帧中放置一个小部件。

Here is a simple example with the control buttons packed in the ROOT frame and two stacked frames, with their contents, gridded on top of each other in a seperate frame. 这是一个简单的示例,其中控制按钮包装在ROOT框架中,两个堆叠的框架及其内容在一个单独的框架中彼此重叠。

import tkinter as tk

ROOT = tk.Tk()

# seperate frame to hold frames stacked on top of each other
stacking_frame = tk.Frame()
stacking_frame.pack()

# two stacked frames within the stacking frame
conj_frame  = tk.Frame(master=stacking_frame)
conj_frame.grid(column=0, row=0)
trans_frame = tk.Frame(master=stacking_frame)
trans_frame.grid(column=0, row=0)

# example contents in each stacked frame
tk.Label(master=conj_frame,
         text='Conj').grid()
tk.Label(master=trans_frame,
         text='Trans').grid()

# buttons commands to alter stacking order
tk.Button(text="Raise Conjugator", command=conj_frame.tkraise).pack()
tk.Button(text="Raise Translator", command=trans_frame.tkraise).pack()

ROOT.mainloop()

Useful introductory information on tkinter can be found here https://tkdocs.com/tutorial/index.html 有关tkinter的有用的入门信息,请参见https://tkdocs.com/tutorial/index.html

A detailed example of frame switching and related information can be found here Switch between two frames in tkinter 可以在此处找到帧切换和相关信息的详细示例, 在tkinter中在两个帧之间切换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM