self.top = top self.frame = tk.Frame(self.top, bg='Gold', width=1100, height=234, highlightthickness=2, highlightbackground='Black') self.frame.place(x=0, y=670) self.canvas = tk.Canvas(self.frame, width=1077, height=226, bg='Gold') self.canvas.pack(side='left', fill='both', expand=True) self.scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview, orient=tk.VERTICAL) self.scrollbar.pack(side='right', fill='y') self.canvas.configure(yscrollcommand=self.scrollbar.set) self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.Content()为什么这里对鼠标滚轮无效果
时间: 2024-02-21 17:02:24 浏览: 64
这段代码中的滚动条只是绑定了 Canvas 的 yview,但是没有绑定 button_frame 的大小,因此无法对鼠标滚轮产生响应。可以尝试在创建 button_frame 之后,调用 Canvas 的 configure(scrollregion) 方法,将其设置为可滚动的范围,即:
```
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
```
这样就可以让鼠标滚轮对 button_frame 产生响应了。
相关问题
elf.top = top self.frame = tk.Frame(self.top, bg='Gold', width=1102, height=234, highlightthickness=2, highlightbackground='Black') self.frame.place(x=0, y=670) self.Text = tk.Text(self.frame) self.Text.place(x=0, y=670)为什么没有显示TEXT
可能是因为你没有指定 Text 的宽度和高度。你可以尝试添加 Text 的宽度和高度参数:
```
self.Text = tk.Text(self.frame, width=50, height=10)
self.Text.place(x=0, y=0)
```
同时,你在将 Frame 放置在窗口上时,y 值设置为了 670,这可能会导致 Frame 和 Text 超出窗口的范围。你可以调整 y 值来确保 Frame 和 Text 在窗口范围内可见。
import tkinter as tk class Three_layout(): def __init__(self, top): self.top = top self.frame = tk.Frame(self.top, bg='Gold', width=1100, height=234, highlightthickness=2, highlightbackground='Black') self.frame.place(x=0, y=670) self.canvas = tk.Canvas(self.frame, width=1077, height=226, bg='Gold') self.canvas.pack(side='left', fill='both', expand=True) self.scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview, orient=tk.VERTICAL) self.scrollbar.pack(side='right', fill='y') self.canvas.configure(yscrollcommand=self.scrollbar.set) self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.Content() self.Text_Add() def Content(self): self.text = tk.Text(self.button_frame, width=1077) self.text.pack(side='left', fill='both', expand=True) self.text.insert('end', f'\n') self.text.config(state='disabled') self.button_frame.update_idletasks() self.canvas.configure(scrollregion=self.canvas.bbox("all")) def Text_Add(self): self.text.insert('end', "Hello, world!")为什么没有显示hello,world
在你的代码中,你在`Text_Add()`方法中调用了`insert()`方法,但是你没有将`state`设置为`normal`,这会导致你无法在`Text`控件中插入文本。所以你需要在调用`insert()`方法之前,将`state`设置为`normal`,插入完文本之后再将`state`设置为`disabled`,以保持`Text`控件的只读状态。修改后的代码如下:
```
import tkinter as tk
class Three_layout():
def __init__(self, top):
self.top = top
self.frame = tk.Frame(self.top, bg='Gold', width=1100,
height=234, highlightthickness=2, highlightbackground='Black')
self.frame.place(x=0, y=670)
self.canvas = tk.Canvas(self.frame, width=1077, height=226, bg='Gold')
self.canvas.pack(side='left', fill='both', expand=True)
self.scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview, orient=tk.VERTICAL)
self.scrollbar.pack(side='right', fill='y')
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.button_frame = tk.Frame(self.canvas, bg='Yellow')
self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw")
self.Content()
self.Text_Add()
def Content(self):
self.text = tk.Text(self.button_frame, width=1077)
self.text.pack(side='left', fill='both', expand=True)
self.text.insert('end', f'\n')
self.text.config(state='disabled')
self.button_frame.update_idletasks()
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def Text_Add(self):
self.text.config(state='normal')
self.text.insert('end', "Hello, world!")
self.text.config(state='disabled')
root = tk.Tk()
app = Three_layout(root)
root.mainloop()
```
在修改后的代码中,我将`Text_Add()`方法中的`self.text.config(state='normal')`添加了进去,以便在调用`insert()`方法之前将`state`设置为正常状态。
阅读全文