我的程序包括鼠标绘图:在顶层窗口上同时再现绘制的曲线。我的目标是将垂直和水平滚动条设置到顶层窗口。
绘图按照我的预期工作,除了我没有看到滚动条以及我收到此错误(但不会停止程序):
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1523, in yview
res = self.tk.call(self._w, 'yview', *args)
TclError: unknown option "0.0": must be moveto or scroll
该计划包括以下几行:
from Tkinter import *
import numpy as np
import cv2
import Image, ImageTk
class Test:
def __init__(self, parent):
self.parent = parent
self.b1="up"
self.xold=None
self.yold=None
self.liste=[]
self.top = TopLevelWindow()
self.s=400,400,3
self.im=np.zeros(self.s,dtype=np.uint8)
cv2.imshow("hello",self.im)
def test(self):
self.drawingArea=Canvas(self.parent,width=400,height=400)
self.drawingArea.pack()
self.drawingArea.bind("<Motion>",self.motion)
self.drawingArea.bind("<ButtonPress-1>",self.b1down)
self.drawingArea.bind("<ButtonRelease-1>",self.b1up)
def b1down(self,event):
self.b1="down"
def b1up(self,event):
self.b1="up"
self.xold=None
self.yold=None
self.liste.append((self.xold,self.yold))
def motion(self,event):
if self.b1=="down":
if self.xold is not None and self.yold is not None:
event.widget.create_line(self.xold,self.yold,event.x,event.y,fill="red",width=3,smooth=TRUE)
self.top.draw_line(self.xold,self.yold,event.x,event.y)
self.xold=event.x
self.yold=event.y
self.liste.append((self.xold,self.yold))
class TopLevelWindow(Frame):
def __init__(self):
Frame.__init__(self)
self.top=Toplevel()
self.top.wm_title("Second Window")
self.canvas=Canvas(self.top,width=400,height=400)
self.canvas.grid(row=0,column=0,sticky=N+E+S+W)
self.sbarv=Scrollbar(self,orient=VERTICAL)
self.sbarh=Scrollbar(self,orient=HORIZONTAL)
self.sbarv.config(command=self.canvas.yview)
self.sbarh.config(command=self.canvas.xview)
self.canvas.config(yscrollcommand=self.canvas.yview)
self.canvas.config(xscrollcommand=self.canvas.xview)
self.sbarv.grid(row=0,column=1,sticky=N+S)
self.sbarh.grid(row=1,column=0,sticky=W+E)
self.canvas.config(scrollregion=(0,0,400,400))
def draw_line(self, xold, yold, x, y):
self.canvas.create_line(xold,yold,x,y,fill="blue",width=3,smooth=TRUE)
if __name__=="__main__":
root = Tk()
root.wm_title("Main Window")
v = Test(root)
v.test()
root.mainloop()
答案 0 :(得分:2)
这些行不正确:
self.canvas.config(yscrollcommand=self.canvas.yview)
self.canvas.config(xscrollcommand=self.canvas.xview)
当画布滚动时,您告诉画布滚动画布。 yscrollcommand
和xscrollcommand
选项通常需要调用滚动条的set
方法:
self.canvas.config(yscrollcommand=self.sbarv.set)
self.canvas.config(xscrollcommand=self.sbarh.set)
答案 1 :(得分:0)
我想分享我发现的解决方案,以防将来有人遇到这个问题:
我只将两个滚动条包围到与画布本身相同的父窗口小部件中。我的意思是:
self.sbarv=Scrollbar(self.top,orient=VERTICAL)
self.sbarh=Scrollbar(self.top,orient=HORIZONTAL)