盡管我已經定義了按鈕的寬度和高度,但它們似乎自己占據了整個框架。我無法理解其背后的可能原因。
import tkinter as tk
root = tk.Tk()
root.geometry('400x400')
root.columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
main_frame = tk.Frame(root, bg="gray")
main_frame.grid(sticky='news')
f2 = tk.Frame(main_frame) # create a frame inside the main frame to attach scroll bar to
f2.grid(row=1, column=1, padx=5, pady=5, sticky='nw', columnspan=3) # for north, west
f2.grid_rowconfigure(0, weight=1)
f2.grid_columnconfigure(0, weight=1)
c2 = tk.Canvas(f2, bg="orange")
c2.grid(row=0, column=0, sticky="nsew")
hsb = tk.Scrollbar(f2, orient="horizontal", command=c2.xview, width=10)
hsb.grid(row=1, column=0, sticky='we') # west-east
c2.configure(xscrollcommand=hsb.set)
f2_c2 = tk.Frame(c2, bg="yellow")
c2.create_window((0, 0), window=f2_c2, anchor='nw')
b1 = tk.Button(f2_c2, relief='raised', width=200, height=200)
b1.grid(row=0, column=0 ,padx=5, pady=5)
b2 = tk.Button(f2_c2, relief='raised', width=200, height=200)
b2.grid(row=0, column=1, padx=5, pady=5)
b3 = tk.Button(f2_c2, relief='raised', width=200, height=200)
b3.grid(row=0, column=2, padx=5, pady=5)
f2_c2.update_idletasks()
c2.configure(scrollregion=c2.bbox("all"), width=400, height=400)
root.mainloop()

有解決方法還是我遺漏了什么?
uj5u.com熱心網友回復:
除非按鈕上有影像,否則按鈕(和其他一些)的width和height屬性被記錄為以平均大小的字符為單位。因此,當您將寬度設定為 200 時,即為 200 個字符。如果一個字符的寬度為 16 像素,則您將按鈕的寬度設定為 3200 像素。
如果要以像素為單位指定寬度,可以向按鈕添加一個不可見像素。
img = tk.PhotoImage()
b1 = tk.Button(f2_c2, relief='raised', width=200, height=200, image=img, compound="c")
b2 = tk.Button(f2_c2, relief='raised', width=200, height=200, image=img, compound="c")
b3 = tk.Button(f2_c2, relief='raised', width=200, height=200, image=img, compound="c")
uj5u.com熱心網友回復:
#BUTTON
image_1 = tk.PhotoImage()
button_1 = tk.Button(f2_c2, relief='raised', width=200, height=200, image=image_1, compound="c")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371744.html
標籤:特金特
下一篇:Tkinter影片變得越來越快
