self.canvas = Canvas(self.top, bg='Olive', width=int(data_demo.window_width // 6.17), height=int(data_demo.window_height // 1.007), highlightthickness=2, highlightbackground='Black') self.canvas.place(x=1100, y=0) # 绘制画布这个的place改成pack怎么弄
时间: 2024-03-17 12:43:36 浏览: 49
将`place(x=1100, y=0)`改为`pack(side='right')`即可将`Canvas`小部件放置在窗口的右侧,并让它自适应窗口大小。
下面是修改后的代码:
```
self.canvas = Canvas(self.top, bg='Olive', width=int(data_demo.window_width // 6.17), height=int(data_demo.window_height // 1.007), highlightthickness=2, highlightbackground='Black')
self.canvas.pack(side='right') # 绘制画布
```
`pack(side='right')`将`Canvas`小部件放置在窗口的右侧,同时让它自适应窗口大小。如果您需要将小部件放置在其他位置,可以将`side`参数设置为`left`、`top`或`bottom`,并根据需要调整其他参数。
希望这可以帮助到您!
相关问题
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")
```
self.top = top self.canvas1 = Canvas(self.top, bg='LightSkyBlue', width=int(data_demo.window_width // 1.74), height=int(data_demo.window_height // 32.4), highlightthickness=2, highlightbackground='Black') self.canvas1.pack(pady=0)为什么显示在了画布居中,我想靠右边
如果你想要将画布靠右边显示,你可以使用`side`属性来指定画布的位置。例如,将`self.canvas1.pack(pady=0)`改为`self.canvas1.pack(side=RIGHT, pady=0)`。这将把画布放置在其容器的右侧,并保持其原始高度。如果你想改变画布的高度,请调整`height`参数的值。
阅读全文