2

i am going to create an tkinter gui app, and i know how i want it to look. but after playing around with tkinter, i found no way to toggle between screens when you press buttons down at the bottom. i know it does nothing but below is the simple layout i want to have, and switch between "myframe1" and "myframe2" kind of like the Apple App Store layout. is this possible?

from tkinter import *

tk = Tk()
tk.geometry("300x300")

myframe1 = Frame(tk,background="green",width=300,height=275)
myframe1.pack()

myframe2 = Frame(tk,background="cyan",width=300,height=275)
myframe2.pack()

btnframe = Frame(tk)

btn1 = Button(btnframe,text="screen1",width=9)
btn1.pack(side=LEFT)

btn2 = Button(btnframe,text="screen2",width=9)
btn2.pack(side=LEFT)

btn3 = Button(btnframe,text="screen3",width=9)
btn3.pack(side=LEFT)

btn4 = Button(btnframe,text="screen4",width=9)
btn4.pack(side=LEFT)

myframe1.pack()
btnframe.pack()

tk.mainloop()
4

2 回答 2

1

您是否正在寻找类似选项卡式小部件的东西?您可以按照这里的建议使用forgetpack

这是我在我的代码中使用的一个类:

class MultiPanel():
  """We want to setup a pseudo tabbed widget with three treeviews. One showing the disk, one the pile and
  the third the search results. All three treeviews should be hooked up to exactly the same event handlers
  but only one of them should be visible at any time.
  Based off http://code.activestate.com/recipes/188537/
  """
  def __init__(self, parent):
    #This is the frame that we display
    self.fr = tki.Frame(parent, bg='black')
    self.fr.pack(side='top', expand=True, fill='both')
    self.widget_list = []
    self.active_widget = None #Is an integer

  def __call__(self):
    """This returns a reference to the frame, which can be used as a parent for the widgets you push in."""
    return self.fr

  def add_widget(self, wd):
    if wd not in self.widget_list:
      self.widget_list.append(wd)
    if self.active_widget is None:
      self.set_active_widget(0)
    return len(self.widget_list) - 1 #Return the index of this widget

  def set_active_widget(self, wdn):
    if wdn >= len(self.widget_list) or wdn < 0:
      logger.error('Widget index out of range')
      return
    if self.widget_list[wdn] == self.active_widget: return
    if self.active_widget is not None: self.active_widget.forget()
    self.widget_list[wdn].pack(fill='both', expand=True)
    self.active_widget = self.widget_list[wdn]
于 2013-07-12T18:51:07.903 回答
1

一些让你开始的东西:

def toggle(fshow,fhide):
    fhide.pack_forget()
    fshow.pack()


btn1 = Button(btnframe,text="screen1", command=lambda:toggle(myframe1,myframe2),width=9)
btn1.pack(side=LEFT)

btn2 = Button(btnframe,text="screen2",command=lambda:toggle(myframe2,myframe1),width=9)
btn2.pack(side=LEFT)
于 2013-07-12T19:39:51.877 回答