繁体   English   中英

画布未填充可用空间 tkinter

[英]Canvas not filling available space tkinter

我正在尝试使用 tkinter 作为 gui 界面制作消息传递程序。 在 gui 的左侧,有一个画布,里面有数字 1 - 100。 画布一侧还有一个滚动条可以滚动这些数字。

问题是画布不会填满所有可用空间,并且由于某种原因将自己限制在一个小盒子里。

代码:

from tkinter import *
# *** Scrollable Frame Class ***

class VerticalScrolledFrame(Frame):

    def __init__(self, parent, *args, **kw):
        Frame.__init__(self, parent, *args, **kw)

        vscrollbar = Scrollbar(self, orient=VERTICAL)
        vscrollbar.pack(fill=Y, side=LEFT, expand=FALSE)
        canvas = Canvas(self, bd=0, highlightthickness=0, yscrollcommand=vscrollbar.set, bg="black")
        canvas.pack(side=LEFT, fill=BOTH, expand=TRUE)
        vscrollbar.config(command=canvas.yview)

        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        self.interior = interior = Frame(canvas)
        interior_id = canvas.create_window(0, 0, window=interior,
                                           anchor=NW)
        def _on_mousewheel(event):
            canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
            self.interior.bind_all("<MouseWheel>", _on_mousewheel)
            canvas.bind_all("<MouseWheel>", _on_mousewheel)

        def _configure_interior(event):
            size = (interior.winfo_reqwidth(), interior.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if interior.winfo_reqwidth() != canvas.winfo_width():
                canvas.config(width=interior.winfo_reqwidth())
        interior.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if interior.winfo_reqwidth() != canvas.winfo_width():
                canvas.itemconfigure(interior_id, width=canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

        # *** Create Window ***
root = Tk()
root.geometry("800x545")
root.config(bg="#23272A")
#root.resizable(False, False)
root.title("Secure Message")

# *** Set Friends Frame ***
friends_frame = VerticalScrolledFrame(root)
friends_frame.pack(side=LEFT)

for i in range(100):
    friend_frames = []
    friend_frame = Frame(friends_frame.interior, width=100)
    friend_frames.append(friend_frame)
    friend_frames[-1].pack()
    text = Label(friend_frame, text=i)
    text.pack(side=LEFT)

chatSpace_frame = Frame(root)
chatSpace_frame.pack(side=RIGHT)

# *** GUI ***
chatSpace = Listbox(chatSpace_frame, bg="#2C2F33", fg="white", width = 65, height=29)
chatSpace.grid(row=0, column=1, sticky=E, padx=10)

user_input = Entry(chatSpace_frame, highlightbackground="#2C2F33", bg="grey", width = 64, fg="white")
user_input.grid(row=1, column=1, sticky=E, pady=10, padx=9)

user_input.focus()

root.mainloop()

如果有点难以理解,我很抱歉。 任何帮助将不胜感激!

截图

破桂

左侧带有数字的画布需要填充聊天空间旁边的所有空白灰色空间。

即使您还没有发布运行的代码,但问题似乎很明显不在于画布,而在于它所在的框架。

这是问题所在:

friends_frame.pack(side=LEFT)

您没有让friends_frame展开,因此其中的画布无法展开。 您需要在框架上设置expand和/或fill选项。

问题是friends_frame没有设置为同时填充X 和Y,并且expand 需要设置为true。

例子

# *** Set frame for friends view ***
friends_frame = VerticalScrolledFrame(root, bg="#23272A")
friends_frame.pack(side=LEFT, fill=BOTH, expand=TRUE)

暂无
暂无

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

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