Tkinter文本小部件内的滚动条 [英] Scrollbar in Tkinter inside Text widget

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

问题描述

我在Tkinter的文本小部件内的滚动条中设置有问题.我知道,最好使用网格来定位小部件,但是我想将小部件设置为具有指定高度和宽度的绝对位置(x,y-GUI图片上的红点).

I have problem with set in Scrollbar inside Text widget in Tkinter. I know, that it's preferable to use grid to locate widgets but I want to set my widget in absolute location (x,y - red dot on GUI picture) with specified height and width.

我的代码:

from Tkinter import *
from ttk import *

class NotebookDemo(Frame):

    def __init__(self):      
        Frame.__init__(self)       
        self.pack(expand=1, fill=BOTH)
        self.master.title('Sample')
        self.master.geometry("650x550+100+50")
        self._initUI()

    def _initUI(self):
        self._createPanel()

    def _createPanel(self):

        # create frame inside top level frame
        panel = Frame(self)    
        panel.pack(side=TOP, fill=BOTH, expand=1)

        # create the notebook
        nb = Notebook(panel)
        nb.pack(fill=BOTH, expand=1, padx=2, pady=3)        
        self._FirstTab(nb)


    def _FirstTab(self, nb):

        # frame to hold content
        frame = Frame(nb)

        #textbox
        txtOutput = Text(frame, wrap = NONE, height = 17, width = 70)
        txtOutput.place(x=10, y=75)

        #button
        btnStart = Button(frame, text = 'Start', underline=0)
        btnStart.place(x=220, y=380)

        #scrollbar
        #vscroll = Scrollbar(frame, orient=VERTICAL, command=txtOutput.yview)
        #txtOutput['yscroll'] = vscroll.set
        #vscroll.pack(side=RIGHT, fill=Y)
        #txtOutput.pack(fill=BOTH, expand=Y)

        #add to notebook (underline = index for short-cut character)
        nb.add(frame, text='TAB 1', underline=0, padding=2)

if __name__ == '__main__':
    app = NotebookDemo()
    app.mainloop()

如果我取消注释这部分代码(设置滚动条):

If I uncomment this part of code (set Scrollbar):

vscroll = Scrollbar(frame, orient=VERTICAL, command=txtOutput.yview)
txtOutput['yscroll'] = vscroll.set
vscroll.pack(side=RIGHT, fill=Y)

我的滚动条位于所有窗口中,而不位于文本框中:

My Scrollbar is located inside all window, not inside Text box:

但是,我当然希望在文本框小部件(黑色边框)中具有滚动条. 如果我将打包功能用于文本框:

But of course I want to have the Scrollbar inside the Text box widget (black border). If I use pack function to textbox:

txtOutput.pack(fill=BOTH, expand=Y)

文本窗口小部件填充整个窗口...:

text widget fill in the whole window...:

我真的不知道该如何解决此问题. 任何帮助将不胜感激. 谢谢!

I really don't know how fix this problem. Any help will be appreciated. Thank you!

我当然也可以在Scrollbar中使用place方法,但是我不能更改它们的长度,因为它没有属性长度.

Of course I can use place method with Scrollbar too, but I can't change length of them, because it hasn't attribute length.

vscroll.place(x=573, y=75)

推荐答案

虽然我很少推荐place,但是当您利用配置选项时,它非常强大.例如,您可以使用in_指定要相对于此小部件放置的小部件.您可以使用relx指定相对的x坐标,也可以使用relheight指定高度.

While I rarely recommend place, it is quite powerful when you take advantage of the configuration options. For example, you can use in_ to specify a widget that this widget is to be placed relative to. You can use relx to specify a relative x coordinate, and you can use relheight to specify a height.

您可以尝试以下方法:

vscroll.place(in_=txtOutput, relx=1.0, relheight=1.0, bordermode="outside")

如果您想让滚动条像在某些平台上一样(或过去)嵌入到文本小部件中,我建议将文本小部件和滚动条放在框架中.您可以使用pack来放置框架中的小部件,然后继续使用place将组合放置在您想要的任何位置.

If you want the illusion that the scrollbar is embedded inside the text widget as is (or used to be) common on some platforms, I recommend placing the text widget and scrollbar in a frame.You can use pack to put the widgets in the frame, and continue to use place to place the combination anywhere you want.

例如:

txtFrame = Frame(frame, borderwidth=1, relief="sunken")
txtOutput = Text(txtFrame, wrap = NONE, height = 17, width = 70, borderwidth=0)
vscroll = Scrollbar(txtFrame, orient=VERTICAL, command=txtOutput.yview)
txtOutput['yscroll'] = vscroll.set

vscroll.pack(side="right", fill="y")
txtOutput.pack(side="left", fill="both", expand=True)

txtFrame.place(x=10, y=75)

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

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