使用 place(而不是打包)将滚动条与列表框对齐
Align the scrollbar to the listbox using place (and not pack)
我想从代码中删除任何“pack”的痕迹,只使用“place”,以便将滚动条定位在列表框附近并沿着列表框的整个高度拉伸它。之前是可以的,后来用pack修改了代码。
现在我想删除所有包并只使用地方
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
scrollbar_title = tk.Scrollbar(frame_title)
scrollbar_title.place(x=500, y=1)
scrollbar_title['command'] = listbox_title.yview
listbox_title.config(yscrollcommand=scrollbar_title.set)
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()
您可以将滚动条直接绑定到列表框或文本小部件,然后当您更改父小部件的大小时它会动态改变。
scroll_bar = tk.Scrollbar(frame_title)
scroll_bar.config(command=listbox_title.yview)
listbox_title.config(yscrollcommand=scroll_bar.set)
## For Verticle Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
## For Horzontal Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=0.0, rely=1.0, relwidth=1.0, bordermode="outside")
然后您可以正常放置列表框。
更新代码:
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
## Vertical Scroll Bar ##
scrollbar_title = tk.Scrollbar(frame_title, command=listbox_title.yview, orient="vertical")
listbox_title.config(yscrollcommand=scrollbar_title.set)
scrollbar_title.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()
我想从代码中删除任何“pack”的痕迹,只使用“place”,以便将滚动条定位在列表框附近并沿着列表框的整个高度拉伸它。之前是可以的,后来用pack修改了代码。
现在我想删除所有包并只使用地方
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
scrollbar_title = tk.Scrollbar(frame_title)
scrollbar_title.place(x=500, y=1)
scrollbar_title['command'] = listbox_title.yview
listbox_title.config(yscrollcommand=scrollbar_title.set)
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()
您可以将滚动条直接绑定到列表框或文本小部件,然后当您更改父小部件的大小时它会动态改变。
scroll_bar = tk.Scrollbar(frame_title)
scroll_bar.config(command=listbox_title.yview)
listbox_title.config(yscrollcommand=scroll_bar.set)
## For Verticle Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
## For Horzontal Scrollbar ##
scroll_bar.place(in_=listbox_title, relx=0.0, rely=1.0, relwidth=1.0, bordermode="outside")
然后您可以正常放置列表框。
更新代码:
import tkinter as tk
from tkinter import ttk
from tkinter.scrolledtext import ScrolledText
window = tk.Tk()
window.geometry("800x800")
frame_title = tk.Frame(window)
frame_title.pack(fill='both', expand=True, pady=5, padx=5)
listbox_title = tk.Listbox(frame_title, selectbackground="#960000", selectforeground="white", bg="white", width = 50, height = 20)
listbox_title.place(x=1, y=1)
listbox_title.configure(font=("Arial", 12))
## Vertical Scroll Bar ##
scrollbar_title = tk.Scrollbar(frame_title, command=listbox_title.yview, orient="vertical")
listbox_title.config(yscrollcommand=scrollbar_title.set)
scrollbar_title.place(in_=listbox_title, relx=1.0, relheight=1.0, bordermode="outside")
listbox_title.bind('<Double-Button-1>')
text_download = ScrolledText(window, bg="white", width = 50, height = 10)
text_download.place(x=1, y=500)
text_download.configure(font=("Arial", 14))
window.mainloop()