文本小部件上的水平滚动条不起作用 [英] Horizontal scrollbar on Text widget not working

查看:118
本文介绍了文本小部件上的水平滚动条不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序,可以将长字符串的一个或多个迭代加载到文本字段中.我设置了一个水平滚动条,以使我可以跨过很长的文本字符串.我想我已经在文本小部件和滚动条之间进行了所有设置(至少看起来像我为垂直滚动条工作的其他代码一样),但是滚动条似乎未激活.无论文本多长时间,它基本上都无法工作.

I have a small program that loads one or more iterations of a long string into a text field. I have set up a horizontal scrollbar to let me move across very long strings of text. I think I have everything set up between the text widget and the scrollbar (at least it looks like other code I have for a vertical scrollbar which works) but the scrollbar appears to be inactivated. No matter how long the text gets, it basically doesn't work.

除了水平滚动条之外,有关此代码的其他所有内容似乎都可以正常工作.

Everything else about this code appears to work, except the horizontal scrollbar.

我做错了什么?代码中还有其他内容可以关闭滚动条吗?

What am I doing wrong? Is something else in the code turning the scrollbar off?

    import tkinter as tk
    from tkinter import messagebox

    win=tk.Tk()

    text=tk.Text(win, height=1, font='Helvetica 12')
    text.pack(side='top', padx=5, pady=5,fill='x')
    text.tag_configure('bold', font='Helvetical 12 bold', foreground='red')
    hscroll=tk.Scrollbar(win, orient='horizontal',command=text.xview)
    hscroll.pack(side='top',fill='x')
    text.configure(xscrollcommand=hscroll.set)
    text.configure(state='normal')


    x='(<data field> == <literal>) and ((<data field> == <data field>) or (<data field> == <data field>))'

   def insert_characters():
        global x
        text.configure(state='normal')
        text.insert('end', x) 
        content=text.get("1.0","end-1c")
        messagebox.showinfo('',len(content))

    def delete_characters():
        text.configure(state='normal')
        text.delete('1.0','1.500')
        text.configure(state='disabled')

    def get_field_list(string):
        field_list=[]
        for i in range(len(string)):
            if string[i]=='<':
                start=i
            elif string[i]=='>':
                stop=i
                field_list.append((start, stop))
            else:
                continue
         return field_list

    def highlight_fields(field_list):
        for f in field_list:
            start='1.{0}'.format(f[0])
            stop='1.{0}'.format(f[1]+1)

            text.tag_add('highlight', start, stop)
            text.tag_configure('highlight',background='yellow',
                       font='Helvetica 12 bold')
            messagebox.showinfo('',start+'\n'+stop)
            text.tag_delete('highlight')

    def do_highlights():
        global x
        field_list=get_field_list(x)
        highlight_fields(field_list)


    insertButton=tk.Button(win, text='Insert',
                           command=insert_characters)
    insertButton.pack(side='bottom', padx=5, pady=5)

    deleteButton=tk.Button(win, text='Delete',
                           command=delete_characters)
    deleteButton.pack(side='bottom', padx=5, pady=5)

    highlightButton=tk.Button(win, text='Highlight',
                              command=do_highlights)
    highlightButton.pack(side='bottom', padx=5, pady=5)

    win.mainloop()

推荐答案

您尚未为文本窗口小部件的wrap选项指定值,因此默认情况下,窗口小部件中的文本将环绕在文本窗口的边缘.窗户.如果文字是自动换行的,那么右边距就不会有任何其他内容,因此滚动条是无用的.

You haven't specified a value for the wrap option of the text widget, so by default the text in the widget will wrap at the edge of the window. If text is wrapped, there is never anything beyond the right margin, so scrollbars are useless.

如果您希望滚动条起作用,请通过将窗口小部件的wrap选项设置为字符串"none"来关闭换行.

If you want the scrollbars to work, turn wrapping off by setting the wrap option of the widget to the string "none".

text=tk.Text(win, height=1, font='Helvetica 12', wrap="none")

这篇关于文本小部件上的水平滚动条不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆