简体   繁体   English

使用滚动条闪烁的 Python Tkinter 框架

[英]Python Tkinter Frame flashing with Scrollbar

I'm attempting to create a level selection menu of sorts for a terminal-based game I'm making.我正在尝试为我正在制作的基于终端的游戏创建各种级别选择菜单。 I had it sorting levels into columns of ten, but it occured to me that the menu would get very wide as more levels are added.我将级别分为 10 列,但我突然想到,随着级别的增加,菜单会变得很宽。 Instead, I'm trying to make an alphabetical list that can be scrolled through.相反,我正在尝试制作一个可以滚动的按字母顺序排列的列表。

I've followed several posts' advice about making a scrollable frame, and I've run into a problem I haven't seen before: The window keeps flashing and spazzing out.我遵循了几篇关于制作可滚动框架的建议,但遇到了一个我以前从未见过的问题:窗口不断闪烁和喷出。 It only occurs one I add the line indicated below, but that line also is the one that makes the level buttons appear.它只发生在我添加下面指示的行的情况下,但该行也是使级别按钮出现的行。

def show_levels(self, frames):
    self.menu_title = tk.Label(self,
                               text="  Nonstop Robot  ",
                               fg="blue"
                               )
    if frames:
        self.frame_levels = tk.Frame(self,
                                     relief="sunken",
                                     width=50,
                                     height=100
                                     )
        self.level_canvas = tk.Canvas(self.frame_levels)
        self.level_list = tk.Frame(self.level_canvas)
        self.level_scroll = tk.Scrollbar(self.frame_levels,
                                         orient="vertical",
                                         command=self.level_canvas.yview
                                         )
        self.level_canvas.configure(yscrollcommand=self.level_scroll.set)
        self.frame_menu = tk.Frame(self)

    def level_bind(event):
        self.level_canvas.configure(
            scrollregion=self.level_canvas.bbox("all"),
            width=70,
            height=200
            )
    self.level_buttons = []
    """
    Code that adds the buttons to all the frames.
    """
    for b in self.level_buttons:
        b.pack()
    if frames:
        self.level_scroll.pack(side="right", fill="y")
        self.level_canvas.pack(side="left")
        self.level_canvas.create_window((0, 0),
                                        window=self.frame_levels,
                                        anchor="nw"
                                        )
        self.frame_levels.bind("<Configure>", level_bind)
        self.frame_levels.pack(side="bottom")
        self.level_list.pack()  # This is the line in question
        self.frame_menu.pack(side="top")
    self.menu_title.pack()

I removed the code that actually creates all the buttons, since it's very long.我删除了实际创建所有按钮的代码,因为它很长。 If needed, I can add it.如果需要,我可以添加它。

You can't use both create_window and pack on self.frame_levels , and if you use self.frame_levels.pack , it will not scroll along with the rest of the canvas.你不能同时使用create_windowpackself.frame_levels ,如果你使用self.frame_levels.pack ,它不会随着画布的其余部分滚动。

You also seem to have the problem that you create a canvas that is a child of self.frame_levels , and then later you try to add self.frame_levels to the canvas.您似乎也遇到了问题,您创建了一个作为self.frame_levels子级的self.frame_levels ,然后您尝试将self.frame_levels添加到画布。

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

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