发布于2023-01-18 23:30 阅读(1034) 评论(0) 点赞(0) 收藏(5)
I am trying to create two buttons to switch frame config by raising different Frame above an other. but the tkraise() function doesn't seems working... Please help to check if there are any mistake or concept error of using tkraise.
Thanks for helping
import tkinter as tk
from tkinter import ttk
SUNKABLE_BUTTON = 'SunkableButton.TButton'
class tkinterApp(tk.Tk):
# __init__ function for class tkinterApp
def __init__(self, *args, **kwargs):
# __init__ function for class Tk
tk.Tk.__init__(self, *args, **kwargs)
# initializing frames to an empty array
self.frames = {}
top_container = tk.LabelFrame(self, width=400, height=100, text="item")
top_container.grid(row = 0, column=0, columnspan=2)
mid_container_l = tk.LabelFrame(self, width=200, height=100, text="item_a")
mid_container_l.grid(row = 1, column=0)
mid_container_r = tk.LabelFrame(self, width=200, height=100, text="item_b")
mid_container_r.grid(row = 1, column=1)
but_container = tk.LabelFrame(self, width=400, height=400, text="item")
but_container.grid(row = 2, column=0, columnspan=2, sticky=tk.W)
for F in (One,Two):
frame = F(self)
self.frames[F] = frame
btn_1 = tk.Button(but_container, text="btn_1", command=lambda : self.show_frame(One))
btn_1.pack(side=tk.LEFT)
btn_2 = tk.Button(but_container, text="btn_2", command=lambda : self.show_frame(Two))
btn_2.pack(side=tk.LEFT)
#self.show_frame(One)
def show_frame(self, cont):
frame = self.frames[cont]
print(frame)
frame.tkraise(aboveThis=None)
class One(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
but_container_in = tk.Frame(parent, width=400, height=200, bg="yellow")
but_container_in.grid(row = 3, column=0, columnspan=2, sticky=tk.W)
class Two(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
but_container_in_l = tk.Frame(parent, width=400, height=200, bg="green")
but_container_in_l.grid(row = 3, column=0, columnspan=2, sticky=tk.W)
#but_container_in_r = tk.Frame(parent, width=200, height=200, bg="red")
#but_container_in_r.grid(row = 3, column=1, columnspan=2, sticky=tk.W)
# Driver Code
app = tkinterApp()
app.mainloop()
I am expecting someone can point out mine blind spot
tkraise
工作中。问题是您正在提升屏幕上不可见的帧,因此没有视觉指示表明它正在工作。
问题出在您的类One
和Two
. 这些类构成两个框架:类本身的实例,然后是一个内部框架。两者都是 的子级parent
,但只有内部框架被添加到窗口(在本例中为grid
)。
我不明白你为什么要在一个本身就是框架的类中创建一个内部框架。至少,那些内部框架应该是班级的孩子而不是父母的孩子。
当您将内部框架设为 的子级self
并调用grid
该类的实例时,您的代码就可以工作了。
class One(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid(row = 3, column=0, columnspan=2, sticky=tk.W)
but_container_in = tk.Frame(self, width=400, height=200, bg="yellow")
but_container_in.pack(fill="both", expand=True)
class Two(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.grid(row = 3, column=0, columnspan=2, sticky=tk.W)
but_container_in_l = tk.Frame(self, width=400, height=200, bg="green")
but_container_in_l.pack(fill="both", expand=True)
注意事项:
self
,因为这些类的实例本身就是框架,而这些类的实例就是您需要调用的那些tkraise
。grid
上self
而不是内部框架上。pack
因为这是让框架填充其父框架的最简单方法作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/1883696/62aff39f53ed72b334c6/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!