简体   繁体   中英

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() 

Every time I change font via function, size of letter changes, but the box is only 1 character wide. In my previous attempt, I managed to fit window to size of the screen, only until font and font size is changed.

I tried doing this: Prevent Tkinter Text widget from resizing on font change This: How to stop Tkinter Text widget resize on font change? And this: Using grid_propagate(False) in tkinter

None seems to work in my case, I am new to Tkinter and I stuck here for 2 days now.

You need to call reader_frame.grid_rowconfigure(0, weight=1) and reader_frame.grid_columnconfigure(0, weight) to allocate all the available space of reader_frame to the Text widget reader . Also you need to add sticky="nsew" to reader.grid(...) to expand the widget to fill the available space as well:

...
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
...

However it is more easier to use .pack() as suggested in the solution in the first posted link:

...
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)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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