【问题标题】:Is it possible in tkinter to pull up different screens in the same locationtkinter 是否可以在同一位置拉出不同的屏幕
【发布时间】:2013-07-12 18:32:34
【问题描述】:

我将创建一个 tkinter gui 应用程序,并且我知道我希望它看起来如何。但是在玩过 tkinter 之后,当你按下底部的按钮时,我发现无法在屏幕之间切换。我知道它什么都不做,但下面是我想要的简单布局,并在“myframe1”和“myframe2”之间切换,有点像 Apple App Store 布局。这可能吗?

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()

【问题讨论】:

    标签: python widget tkinter switch-statement frame


    【解决方案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)
    

    【讨论】:

    • 您也可以使用 place 而不是 pack,因为 place 可以让您将小部件堆叠在一起。然后你可以lift()你想被看到的那个。
    • 当我将切换功能放在我的代码中进行测试时,按钮在切换或切换后会上升到顶部。即使我在切换函数中说 btnframe.pack() ,它仍然停留在顶部
    【解决方案2】:

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

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

    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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多