self.frame_left_top.grid_propagate(0)的注释
时间: 2023-02-05 19:20:10 浏览: 53
`self.frame_left_top.grid_propagate(0)` 的作用是让左上方的框架固定大小。
`grid_propagate` 方法用来设置是否允许窗口管理器调整小部件大小。如果设为 `0`,则窗口管理器将不会调整小部件大小。如果设为 `1`,则窗口管理器将调整小部件大小以适应窗口大小。
在这里,设置为 `0` 后,就可以通过其他方法(如 `grid_rowconfigure` 和 `grid_columnconfigure`)来调整左上方的框架的大小。
相关问题
class Lnput_lnitialization(): def __init__(self, top, Number_of_rows): self.top = top int(data_demo.window_width // 3.9875) int(data_demo.window_height // 1.138) self.frame = tk.Frame(self.top, bg='Black', highlightthickness=2, highlightbackground='Black') self.frame.place(relx=0.739, rely=0.083, width=int(data_demo.window_width // 3.87), height=int(data_demo.window_height // 1.15)) self.canvas = tk.Canvas(self.frame) self.canvas.place(relx=0, rely=0, width=int(data_demo.window_width // 3.9875), height=int(data_demo.window_height // 1.15)) scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview) scrollbar.pack(side="right", fill="y") self.canvas.configure(yscrollcommand=scrollbar.set) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.button_frame.pack_propagate(False) # 禁止自动调整大小 self.button_frame.pack(fill=BOTH, expand=True) # 使其自适应大小 self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.button_frame.columnconfigure(0, weight=1) self.button_frame.columnconfigure(1, weight=1) self.button_frame.columnconfigure(2, weight=1) self.button_frame.columnconfigure(3, weight=1) self.button_frame.columnconfigure(4, weight=1) self.button_frame.columnconfigure(5, weight=1) self.Content(Number_of_rows)为什么用不了滚轮了
这段代码中使用了tkinter的Canvas组件来创建一个可滚动的窗口,但是在代码中并没有对Canvas绑定滚轮事件的处理程序。因此,当鼠标滚轮事件发生时,Canvas并没有响应。解决方法是在代码中使用canvas.bind()方法绑定滚轮事件,并在绑定的处理程序中调用Canvas的yview()方法来实现滚动。具体实现可以参考下面的示例代码:
```python
class Lnput_lnitialization():
def __init__(self, top, Number_of_rows):
self.top = top
int(data_demo.window_width // 3.9875)
int(data_demo.window_height // 1.138)
self.frame = tk.Frame(self.top, bg='Black', highlightthickness=2, highlightbackground='Black')
self.frame.place(relx=0.739, rely=0.083, width=int(data_demo.window_width // 3.87), height=int(data_demo.window_height // 1.15))
self.canvas = tk.Canvas(self.frame)
self.canvas.place(relx=0, rely=0, width=int(data_demo.window_width // 3.9875), height=int(data_demo.window_height // 1.15))
scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview)
scrollbar.pack(side="right", fill="y")
self.canvas.configure(yscrollcommand=scrollbar.set)
self.button_frame = tk.Frame(self.canvas, bg='Yellow')
self.button_frame.pack_propagate(False) # 禁止自动调整大小
self.button_frame.pack(fill=BOTH, expand=True) # 使其自适应大小
self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw")
self.button_frame.columnconfigure(0, weight=1)
self.button_frame.columnconfigure(1, weight=1)
self.button_frame.columnconfigure(2, weight=1)
self.button_frame.columnconfigure(3, weight=1)
self.button_frame.columnconfigure(4, weight=1)
self.button_frame.columnconfigure(5, weight=1)
self.Content(Number_of_rows)
self.canvas.bind("<MouseWheel>", self.on_mousewheel)
def on_mousewheel(self, event):
self.canvas.yview_scroll(-1 * int(event.delta / 120), "units")
```
class Lnput_lnitialization(): def __init__(self, top, Number_of_rows): self.top = top int(data_demo.window_width // 3.9875) int(data_demo.window_height // 1.138) self.frame = tk.Frame(self.top, bg='Black', highlightthickness=2, highlightbackground='Black') self.frame.place(relx=0.739, rely=0.083, width=int(data_demo.window_width // 3.87), height=int(data_demo.window_height // 1.15)) self.canvas = tk.Canvas(self.frame) self.canvas.place(relx=0, rely=0, width=int(data_demo.window_width // 3.9875), height=int(data_demo.window_height // 1.15)) scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview) scrollbar.pack(side="right", fill="y") self.canvas.configure(yscrollcommand=scrollbar.set) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.Content(Number_of_rows) # 在步骤栏创建填写框 def Content(self, Number_of_rows): x = 0.01 y = 0.01 for a in range(Number_of_rows): var = tk.IntVar() checkbutton1 = Checkbutton(self.button_frame, variable=var, text="{}".format(data_demo.Serial_Number), compound='right') checkbutton1.grid(row=data_demo.Serial_Number, column=1, padx=3, pady=5) data_demo.checkbutton1_boxs.append(checkbutton1) data_demo.checkbutton_bool.append(var) combobox1 = ttk.Combobox(self.button_frame, values=["遥控", "语音", "随机"], width=4) combobox1.grid(row=data_demo.Serial_Number, column=2, padx=6, pady=5) data_demo.comboxox_boxs.append(combobox1)怎么让他自适应大小
可以考虑使用`pack_propagate(False)`方法来禁止自动调整大小,然后使用`pack(fill=BOTH, expand=True)`来使其自适应大小。具体修改方法如下:
1. 将`self.button_frame`的`pack()`方法更改为`pack_propagate(False)`,以禁止自动调整大小。
2. 在`self.canvas.create_window()`方法中添加`pack(fill=BOTH, expand=True)`,以使其自适应大小。
修改后的代码如下:
```
self.button_frame = tk.Frame(self.canvas, bg='Yellow')
self.button_frame.pack_propagate(False) # 禁止自动调整大小
self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw")
self.button_frame.pack(fill=BOTH, expand=True) # 使其自适应大小
```