import tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=200, height=200) canvas.pack() rect = canvas.create_rectangle(50, 50, 150, 150, fill="blue") button = tk.Button(root, text="停止动画", command=root.quit) button.pack() dx = 5 dy = 5 while True: canvas.move(rect, dx, dy) canvas.update() # 如果矩形碰到了边界,就反弹回来 if canvas.coords(rect)[0] <= 0 or canvas.coords(rect)[2] >= 200: dx = -dx if canvas.coords(rect)[1] <= 0 or canvas.coords(rect)[3] >= 200: dy = -dy # 等待一段时间,让动画看起来更流畅 root.after(10) root.mainloop()
时间: 2023-05-22 11:02:15 浏览: 245
这段代码使用了Python的GUI库tkinter,创建了一个窗口和一个画布,并在画布上绘制了一个蓝色的矩形。同时还创建了一个按钮,点击该按钮可以停止动画。在while循环中,矩形会以每次移动5个像素的速度向右下方移动,直到程序被手动停止。
相关问题
import tkinter as tk root = tk.Tk() # 创建一个画布和一个滚动条 canvas = tk.Canvas(root, width=400, height=300) scrollbar = tk.Scrollbar(root, orient='vertical', command=canvas.yview) canvas.config(yscrollcommand=scrollbar.set) scrollbar.pack(side='right', fill='y') canvas.pack(side='left', fill='both', expand=True) # 在画布中添加一个文本框 text = tk.Text(canvas) text.pack(side='left', fill='both', expand=True) canvas.create_window((0, 0), window=text, anchor='nw') # 添加一些文本到文本框中 for i in range(50): text.insert('end', f'Line {i}\n') # 配置文本框的滚动区域 text.config(yscrollcommand=scrollbar.set) text.config(height=10) text.config(width=30) root.mainloop()为什么滚轮不能鼠标滑动
这是因为你需要将滚轮绑定到 canvas 上才能使其滚动。可以使用以下代码将滚轮绑定到 canvas 上:
```
canvas.bind_all("<MouseWheel>", lambda event: canvas.yview_scroll(int(-1*(event.delta/120)), "units"))
```
将上述代码添加到你的程序中,就可以使用鼠标滚轮来滚动 canvas 了。
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`设置为正常状态。
阅读全文
相关推荐
















