Tkinter - 画布文本对象自动滚动(Tkinter - Canvas Text Object Auto-Scroll)
我正在使用Canvas和在此Canvas顶部绘制的create_text对象的Tkinter应用程序。
此文本对象词通过width属性自动换行。 但是,我希望此文本在文本对象包装到第4行时自动剪切文本的顶行。
所以从这里开始:
aaaa
bbbbbb
cccc
dddddd
对此:
bbbbbb
cccc
dddddd
有什么好方法吗?
I'm working on a Tkinter application with a Canvas and a create_text object drawn on top of this Canvas.
This text object word wraps automatically through the width property. However, I would like this text to automatically clip the top line of the text when the text object wraps to a 4th line.
So go from this:
aaaa
bbbbbb
cccc
dddddd
To this:
bbbbbb
cccc
dddddd
Any good way to do this?
原文:https://stackoverflow.com/questions/45201599
更新时间:2019-11-19 02:45
最满意答案
有什么好方法吗?
不,没有办法让画布文本项按照您想要的方式剪辑。
Any good way to do this?
No, there is no way to cause canvas text items to clip the way you want.
2017-07-19
相关问答
问题是您将画布设置为滚动条父级: scrollbar = Scrollbar(c) ,因此当您对滚动条进行网格化时,画布会缩小以适合滚动条。 因此,解决方案是为画布和滚动条使用相同的父级并将它们并排网格化: c = Canvas(root, bg="red", width=900, height=500, scrollregion=(1,0,1000,1000))
scrollbar = Scrollbar(root)
c.grid(row=1, column=0)
sc
...
有什么好方法吗? 不,没有办法让画布文本项按照您想要的方式剪辑。 Any good way to do this? No, there is no way to cause canvas text items to clip the way you want.
要将滚动条连接到小部件,您必须做两件事:告诉小部件哪个滚动条与(与xscrollcommand和/或yscrollcommand )进行交互,并告诉滚动条滚动哪个小部件(使用command属性)。您忽略了执行第一部分。 创建滚动条后在某处添加以下内容: photolist.configure(xscrollcommand=scrl.set)
注意:如果您只想滚动某些图像,则不需要在画布中嵌入框架。 您可以直接在画布上创建图像。 框架给你的好处是你可以使用pack所以你不必计算图像放置位置的坐标,
...
问题是B.pack() - B在那一点是一个按钮,是你创建的最后一个按钮(所以你对该行的评论完全错误)。 打包将其从原先放置在其中的画布的控制中移开,并将其作为独立项目放置在其父控件中。 The problem is the line B.pack() - B at that point is a single button, the very last one you created (so your comment on that line is utterly wrong). Packing
...
不,您不需要按钮来注册鼠标点击。 画布可以捕捉这些事件本身。 您可以在这些行中添加一些内容: def callback(event):
cv.focus_set()
print("clicked at {} {}".format(event.x, event.y))
cv.bind("", callback)
cv.pack()
然后,您可以使用收到的坐标来测试您是否在您的某个圈内,并采取适当的措施。 顺便说一下,你确定你应该运行cv.mainloop()而
...
你的滚动条的高度与按钮的框架高度不匹配,因为你没有告诉它粘贴南北的.grid(..., sticky='ns') 然后,此处描述了您要实现的滚动行为: 将滚动条添加到一组窗口小部件 另请参阅@ martineau针对2D滚动的更一般的面向对象解决方案(水平和垂直) import tkinter as tk
root = tk.Tk()
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
frame_main
...
解决方案非常简单:绑定到画布的事件 - 这将导致在调整画布窗口小部件时调用回调。 然后,您只需要获取画布的宽度并使用它来迭代调整所有嵌入窗口的大小。 The solution is pretty simple: bind to the event of the canvas -- this will cause your callback to be called whenever the canvas widget is resized. You t
...
鼠标事件在“屏幕坐标”中报告。 当你有一个滚动的画布时,你经常需要将这些数字转换为'canvas(即.rollregion)坐标'。 例如。 为您的变焦焦点: true_x = canvas.canvasx(event.x)
true_y = canvas.canvasy(event.y)
Mouse events are reported in 'screen coordinates'. When you have a scrolled canvas, you often need to co
...
我认为问题来自这里: self.win.update_idletasks()
self.ca.configure(scrollregion = (1,1,win.winfo_width(),win.winfo_height()))
这是在初始化函数内部,它应该在调用create函数后更新。 仍然可能有一种更有效的方法来构建它,但这应该在此期间起作用: from Tkinter import *
class new_window(Toplevel):
def __init__(self
...
你的滚动条工作正常。 但是,您已定义的scrollregion与画布的大小完全相同。 因此没有什么可以滚动到。 尝试 canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,800))
你会看到你可以向下滚动300像素。 完整的工作示例代码: app = Tk()
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
can
...