簡體   English   中英

tkinter,如何防止Text widget在更改字體和設置widget大小時調整大小?

[英]In tkinter, how to prevent Text widget from resizing when font is changed and set size for widget?


from tkinter import *

# main window
root = Tk()
root.state('zoomed')
root.geometry("1200x800")

frame_left = Frame(root, padx=10, pady=10)
frame_left.grid(column=0, row=0, columnspan=1) #buttons

# button_get = Button(frame_left, text="Grab posts").grid(column=0, row=0)

Button(frame_left, text="Hot", command=lambda: get_json(2, 0)).grid(column=0, row=2)
Button(frame_left, text="New", command=lambda: get_json(3, 0)).grid(column=0, row=3)
Button(frame_left, text="Controversial", command=lambda: get_json(0, 0)).grid(column=0, row=4)
Button(frame_left, text="Top today", command=lambda: get_json(6, 1)).grid(column=0, row=5)
Button(frame_left, text="Top week", command=lambda: get_json(6, 2)).grid(column=0, row=6)
Button(frame_left, text="Top month", command=lambda: get_json(6, 3)).grid(column=0, row=7)
Button(frame_left, text="Top year", command=lambda: get_json(6, 4)).grid(column=0, row=8)
Button(frame_left, text="Top all time", command=lambda: get_json(6, 5)).grid(column=0, row=9)

# separator = ttk.Separator(frame_left, orient="horizontal").grid(column=0, row=1)

for widget in frame_left.winfo_children():
    widget.grid(padx=8, pady=8, sticky="WE")

frame_right = Frame(root, padx=10, pady=10)
frame_right.grid(column=2, row=0, columnspan=2) #textbox

frame_center = Frame(root, padx=10, pady=10)
frame_center.grid(column=0, row=1) #listbox

listbox = Listbox(frame_center, width=40, height=20)
listbox.insert(0, "This list is currently empty. Click on some of the buttons on the left to grab new posts.")
listbox.grid(column=0, row=1)
listbox.bind('<<ListboxSelect>>', lambda x: get_story())

scrollbar = Scrollbar(frame_center, orient='horizontal', command=listbox.xview)
scrollbar.grid(row=2, column=0, sticky="WE")

reader_frame = Frame(frame_right, width=400, height=400)
reader_frame.grid_propagate(0)
reader_frame.grid(row=0, column=0)

reader = Text(reader_frame, wrap="word", width=1, height=1)
reader.grid(column=0, row=0)
reader.configure(state='disabled')

root.mainloop() 

每次我通過 function 更改字體時,字母的大小都會改變,但框只有 1 個字符寬。 在我之前的嘗試中,我設法使 window 適合屏幕大小,直到更改字體和字體大小。

我試過這樣做: Prevent Tkinter Text widget from resizing on font change This: How to stop Tkinter Text widget resize on font change? 還有這個: 在 tkinter 中使用 grid_propagate(False)

我的情況似乎沒有任何效果,我是 Tkinter 的新手,現在我在這里呆了 2 天。

您需要調用reader_frame.grid_rowconfigure(0, weight=1)reader_frame.grid_columnconfigure(0, weight)reader_frame的所有可用空間分配給Text widget reader 您還需要將sticky="nsew"添加到reader.grid(...)以擴展小部件以填充可用空間:

...
reader_frame = Frame(frame_right, width=400, height=400)
reader_frame.grid_propagate(0)
reader_frame.grid(row=0, column=0)

# allocate all space of reader_frame to row 0 and column 0
reader_frame.grid_rowconfigure(0, weight=1)
reader_frame.grid_columnconfigure(0, weight=1)

reader = Text(reader_frame, wrap="word", width=1, height=1)
reader.grid(column=0, row=0, sticky="nsew") # expand to fill available space
...

但是,按照第一個發布鏈接的解決方案中的建議,使用.pack()更容易:

...
reader_frame = Frame(frame_right, width=400, height=400)
reader_frame.pack_propagate(0)
reader_frame.grid(row=0, column=0)

reader = Text(reader_frame, wrap="word", width=1, height=1)
reader.pack(fill="both", expand=True)
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM