【发布时间】:2015-05-29 14:39:09
【问题描述】:
我的程序包括鼠标绘图:绘制曲线的同时再现是在顶层窗口上完成的。我的目标是将垂直和水平滚动条设置为顶层窗口。
绘图按我的预期工作,除了我没有看到滚动条以及我收到此错误(但不会停止程序):
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()
【问题讨论】:
-
看起来像
self.canvas.config(yscrollcommand=self.canvas.yview)行和它后面的行是问题所在。不过不知道怎么解决。 -
@Kevin 谢谢。它修复了错误,但没有修复第二个问题:滚动条仍未显示。其他一切正常
标签: python python-2.7 tkinter