栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

用鼠标移动Tkinter画布

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用鼠标移动Tkinter画布

canvas具有通过

scan_mark
scan_dragto
方法使用鼠标滚动的内置支持。前者会记住您单击鼠标的位置,而后者会在窗口中滚动适当数量的像素。

注意:该

gain
属性告诉您
scan_moveto
鼠标移动每个像素要移动多少像素。默认情况下为10,因此如果要在光标和画布之间建立1:1的关联,则需要将此值设置为1(如示例中所示)。

这是一个例子:

import Tkinter as tkimport randomclass Example(tk.frame):    def __init__(self, root):        tk.frame.__init__(self, root)        self.canvas = tk.Canvas(self, width=400, height=400, background="bisque")        self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)        self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)        self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)        self.canvas.configure(scrollregion=(0,0,1000,1000))        self.xsb.grid(row=1, column=0, sticky="ew")        self.ysb.grid(row=0, column=1, sticky="ns")        self.canvas.grid(row=0, column=0, sticky="nsew")        self.grid_rowconfigure(0, weight=1)        self.grid_columnconfigure(0, weight=1)        for n in range(50): x0 = random.randint(0, 900) y0 = random.randint(50, 900) x1 = x0 + random.randint(50, 100) y1 = y0 + random.randint(50,100) color = ("red", "orange", "yellow", "green", "blue")[random.randint(0,4)] self.canvas.create_rectangle(x0,y0,x1,y1, outline="black", fill=color)        self.canvas.create_text(50,10, anchor="nw",text="Click and drag to move the canvas")        # This is what enables scrolling with the mouse:        self.canvas.bind("<ButtonPress-1>", self.scroll_start)        self.canvas.bind("<B1-Motion>", self.scroll_move)    def scroll_start(self, event):        self.canvas.scan_mark(event.x, event.y)    def scroll_move(self, event):        self.canvas.scan_dragto(event.x, event.y, gain=1)if __name__ == "__main__":    root = tk.Tk()    Example(root).pack(fill="both", expand=True)    root.mainloop()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/667959.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号