0

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. 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?

Athreya Daniel
  • 101
  • 1
  • 10

1 Answers1

0

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.

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

A detailed example of frame switching and related information can be found here Switch between two frames in tkinter

R Spark
  • 323
  • 1
  • 4
  • 11