简体   繁体   English

滚动条在Tkinter中使用网格布局滚动文本小部件

[英]Scrollbar to scroll Text widget, using Grid layout, in Tkinter

I am trying to use the grid layout manager in Tkinter to create a dialog box. 我试图在Tkinter中使用网格布局管理器来创建一个对话框。 I want the Text area at the bottom to have a scrollbar. 我希望底部的文本区域有一个滚动条。 Unfortunately I can not figure out how to make the scrollbar be the same height as the Text widget beside it. 不幸的是,我无法弄清楚如何使滚动条与它旁边的文本小部件的高度相同。 The sample below shows that the scrollbar is in the right position but is the wrong size (only about 10% of the height of the Text widget). 下面的示例显示滚动条位于正确位置但尺寸错误(仅占Text小部件高度的10%左右)。

tkinter网格和滚动条的布局问题

from tkinter import *

class queryrunner(Tk):
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.minsize(width=800,height=500)
        self.initialize()

    def initialize(self):
        self.grid_columnconfigure(2,weight=1)
        self.grid_columnconfigure(3,weight=1)
        self.grid_columnconfigure(6,weight=2)
        self.grid_rowconfigure(0,weight=1)
        self.grid_rowconfigure(1,weight=1)
        self.grid_rowconfigure(2,weight=1)
        self.grid_rowconfigure(3,weight=1)
        self.grid_rowconfigure(4,weight=1)
        # BUCKET AND N1QL LABEL + INPUT PAIRS: 
        self.label = Label(self,text="Bucket", width=10, anchor="w")
        self.label.grid(column=0,row=0,columnspan=1,sticky='W')
        self.entry = Entry(self);
        self.entry.grid(column=1,row=0, columnspan=3, sticky='EW')
        self.entry.insert(0, "couchbase://couchbase1.mycompany.com/beer-sample" )
        # EXECUTE N1QL QUERY AGAINST BUCKET WHEN BUTTON CLICKED:
        self.button = Button(self,text="Go",width=20)
        self.button.grid(column=6,row=0)
        self.label2 = Label(self,text="N1QL", anchor="w")
        self.label2.grid(column=0,row=1,columnspan=1,sticky='W')
        self.entry2 = Text(self,height=5);
        self.entry2.grid(column=1,row=1, columnspan=3, rowspan=1, sticky='W')

        self.label3 = Label(self,text="Output:", width=50,anchor="w")
        self.label3.grid(column=0,row=4,columnspan=5,sticky='W')
        self.entry3 = Text(self,height=18)
        self.entry3.grid(column=1,row=5, columnspan=5, rowspan=1, sticky='W')
        # PROBLEM: GET SCROLLBAR TO BE THE RIGHT SIZE (IT'S NOT THE SIZE OF THE THING ITS BESIDE)
        self.scrollbar = Scrollbar(self) # height= not permitted here!
        self.entry3.config(yscrollcommand= self.scrollbar.set)
        self.scrollbar.config(command= self.entry3.yview)
        self.grid()
        self.scrollbar.grid(column=6, row=5, rowspan=2,  sticky='W')


if __name__ == "__main__":
    app = queryrunner(None)
    #font.nametofont('TkDefaultFont').configure(size = 10 )
    app.title('Couchbase N1QL Query Runner')
    app.mainloop()

将North和South添加到Scrollbar网格调用的粘性选项,以便滚动条在垂直方向上拉伸。

self.scrollbar.grid(column=6, row=5, rowspan=2,  sticky=N+S+W)

You can only use one geometry manager on each of a container's child widgets, but if a child widget is itself a container, you can use any geometry manager you like for its children. 您只能在容器的每个子窗口小部件上使用一个几何管理器,但如果子窗口小部件本身是容器,则可以使用您喜欢的任何几何管理器作为其子窗口。 This means we can just make a Frame and pack() the Entry and Scrollbar into it, using the fill='y' argument to make the Scrollbar fill the available space. 这意味着我们可以使用fill='y'参数创建一个Framepack() EntryScrollbar ,使Scrollbar填充可用空间。

Change this: 改变这个:

self.entry3 = Text(self,height=18)
self.entry3.grid(column=1,row=5, columnspan=5, rowspan=1, sticky='W')
# PROBLEM: GET SCROLLBAR TO BE THE RIGHT SIZE (IT'S NOT THE SIZE OF THE THING ITS BESIDE)
self.scrollbar = Scrollbar(self) # height= not permitted here!
self.entry3.config(yscrollcommand= self.scrollbar.set)
self.scrollbar.config(command= self.entry3.yview)
self.grid()
self.scrollbar.grid(column=6, row=5, rowspan=2,  sticky='W')

to this: 对此:

self.frame = Frame(self)
self.frame.grid(column=1,row=5, columnspan=6, rowspan=1, sticky='W')
self.entry3 = Text(self.frame,height=18)
self.entry3.pack(side='left', fill='both', expand=True)
# PROBLEM: SOLVED
self.scrollbar = Scrollbar(self.frame) # height= not permitted here!
self.entry3.config(yscrollcommand= self.scrollbar.set)
self.scrollbar.config(command= self.entry3.yview)
self.grid()
self.scrollbar.pack(side='right', fill='y')

There is the ScrolledText module. 有ScrolledText模块。

If you are using Anaconda python distribution then in conda command line do: 如果您使用的是Anaconda python发行版,那么在conda命令行中执行:

 conda install ScrolledText

Then in your .py file import module (if python 3 and up) 然后在你的.py文件导入模块中(如果python 3及以上)

 from tkinter.scrolledtext import ScrolledText

And if python 2.7 then 如果是python 2.7那么

 import ScrolledText

Then you call it like a normal Text widget like so, in python 3: 然后在python 3中将其称为像普通的Text小部件一样:

self.textBox = ScrolledText(self, borderwidth=3, relief="sunken") self.textBox = ScrolledText(self,borderwidth = 3,relief =“sunken”)

It is a compound widget (Text & Scrollbar). 它是一个复合小部件(文本和滚动条)。

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

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