我尝试使用以下视频为我的应用添加滚动条:https://www.youtube.com/watch?v=0WafQCaok6g
当我尝试使用 place 而不是 pack 的小部件时,我的小部件没有显示在窗口中。有人知道这是怎么回事吗?
以下是我的代码片段:
from tkinter import *
from tkinter import ttk
originY = 0
window = Tk()
window.title("Applications Management System")
window.geometry("100x100")
main_frame = Frame(window)
main_frame.pack(fill=BOTH, expand=1)
canvas = Canvas(main_frame)
canvas.pack(side=LEFT, fill=BOTH, expand=1)
scrollBar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=canvas.yview)
scrollBar.pack(side=RIGHT, fill=Y)
canvas.configure(yscrollcommand=scrollBar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
frame = Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor="nw")
companyName = Label(frame, text="XYXYX Company", fg='purple').place(x=0, y=originY)
originY += 30
applicationFormTitle = Label(frame, text="JOB APPLICATION FORM", bg='purple', fg='white', width=90).place(x=0, y=originY)
originY += 30
# application details portion
posApplyingTitle = Label(frame, text="Position Applied").place(x=0, y=originY)
posApplyingTextField = Entry(frame).place(x=100, y=originY, width=200)
dateAppliedTitle = Label(frame, text="Date Applied").place(x=320, y=originY)
dateAppliedTextField = Entry(frame).place(x=420, y=originY, width=200)
originY += 30
...
outcomeTitle = Label(frame, text="Outcome").place(x=0, y=originY)
discardOption = Radiobutton(frame, text="Discard").place(x=100, y=originY)
keepForFutureOption = Radiobutton(frame, text="Keep for Future").place(x=250, y=originY)
originY += 30
insufficientDocOption = Radiobutton(frame, text="Insufficient Documents").place(x=100, y=originY)
callForInterviewOption = Radiobutton(frame, text="Call for Interview").place(x=250, y=originY)
offerWithoutInterviewOption = Radiobutton(frame, text="Offer without Interview").place(x=400, y=originY)
originY += 30
reasonTitle = Label(frame, text="Reason").place(x=0, y=originY)
reasonTextField = Entry(frame).place(x=100, y=originY, width=200)
originY += 30
dateOfReviewTitle = Label(frame, text="Date").place(x=0, y=originY)
dateOfReviewTextField = Entry(frame).place(x=100, y=originY, width=200)
originY += 30
window.mainloop()
滚动条也不起作用。
如果有人能够提供帮助,将不胜感激
答案 0 :(得分:1)
当您使用 pack
或 grid
时,父小部件将增大或缩小以适应子小部件。使用 place
时不会发生这种情况。因此,由于您使用的是 place
,因此 frame
不会增大或缩小以适应其中的小部件。由于您没有明确指定 frame
的大小,因此它默认为 1x1 像素点,因此无法看到。
如果您选择坚持使用 place
,则由您来计算 frame
的适当大小。这是使用 place
的主要缺点:它需要您做更多的工作才能使小部件看起来正确。
更好的选择是对 pack
内的小部件使用 grid
和/或 frame
。这将导致 frame
适合自己的正确尺寸。然后,您可以绑定到框架的 <Configure>
事件以重新计算画布的 scrollregion
,以便可滚动区域完全适合 frame
及其所有子项所需的空间。>