【问题标题】:Issue with horizontally scrollable text in python tkinterpython tkinter中水平滚动文本的问题
【发布时间】:2019-12-09 14:10:53
【问题描述】:

我想在 tkinter 中有一个水平滚动的文本窗口。窗口不时更新,并添加新文本,将现有文本向右移动。问题是当文本到达窗口的末尾时,它不会继续向右移动,而是跳到下面的新行。因此,文本变得可垂直滚动而不是水平滚动。代码如下:

from Tkinter import *

root = Tk()
root.title("PCB Tester ver 0.1")
root.geometry("1000x1000")

data1 = Text(root,height=1,width=20)

scrollbar1=Scrollbar(root, orient=HORIZONTAL)   
scrollbar1.config(command=data1.xview)
scrollbar1.grid(row=1,column=0,sticky=EW)

data1.config(xscrollcommand=scrollbar1.set)
data1.grid(row=0,column=0,sticky = W)

def addText():
    data1.insert('0.0', 'Sample Text')
    root.after(1000, addText)

addText()
root.mainloop()

有什么方法可以让文本继续向右推,这样我就可以使用水平滚动了吗?

【问题讨论】:

标签: python tkinter


【解决方案1】:

您应该在定义 Text 小部件时防止换行:

from Tkinter import *

root = Tk()
root.title("PCB Tester ver 0.1")
root.geometry("1000x1000")

data1 = Text(root,height=1,width=20, wrap="none")

scrollbar1=Scrollbar(root, orient=HORIZONTAL)   
scrollbar1.config(command=data1.xview)
scrollbar1.grid(row=1,column=0,sticky=EW)

data1.config(xscrollcommand=scrollbar1.set)
data1.grid(row=0,column=0,sticky = W)

def addText():
    data1.insert('1.0', 'Sample Text')
    root.after(1000, addText)

addText()
root.mainloop()

【讨论】:

  • FWIW,文本小部件中的第一个索引是"1.0",而不是"0.0"
  • 你是对的 Bryan,选择性阅读欺骗了我 :) 在答案中更正了它。
猜你喜欢
  • 2018-07-04
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多