简体   繁体   English

Tkinter如何检测文本窗口小部件内容是否太长?

[英]Tkinter how to detect if text widget content is too long?

I'm just needing something that can tell if the content in a 1 line text widget is too long, thus needing a horizontal scrollbar. 我只需要一些可以判断1行文本窗口小部件中内容是否太长的东西,因此需要水平滚动条。

I have searched questions others have posted and found a solution for a vertical scrollbar, but it sadly doesn't work as accurately with the horizontal. 我搜索了其他人发布的问题,并找到了针对垂直滚动条的解决方案,但可悲的是,它与水平滚动条不能一样精确。

My solution for vertical: 我的垂直解决方案:

if self.verse_box.cget('height') < int(self.verse_box.index('end-1c').split('.')[0]):
    print "Too many lines"

But if I switch it like so it doesn't work as some characters are smaller and thus can have more fit in the line: 但是,如果我像这样切换它,由于某些字符较小,因此行中可能会更合适,因此它不起作用:

if self.verse_box.cget('width') < int(self.tags_box.index('end-1c').split('.')[1]):
    print "This one line is too long"

Any and all advice would be greatly appreciated, thanks! 任何和所有建议将不胜感激,谢谢! ^-^ ^-^

On any widget that supports scrolling, you can call the .xview() or .yview() method without parameters to get the current start/end fractions of the visible area along that axis. 在任何支持滚动的小部件上,都可以调用不带参数的.xview().yview()方法来获取沿该轴的可见区域的当前开始/结束分数。 If the entire contents are already visible, this will be the tuple (0.0, 1.0) . 如果全部内容已经可见,则为元组(0.0, 1.0) If the returned value is anything else, a scrollbar is needed to see the entire contents. 如果返回的值是其他任何值,则需要滚动条才能查看全部内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM