Lip*_*ick 2 python tkinter scrollbar python-2.7
我在Tkinter的文本小部件内的滚动条中设置有问题。我知道,最好使用网格来定位小部件,但是我想将小部件设置为具有指定高度和宽度的绝对位置(x,y-GUI图片上的红点)。
我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
如果我取消注释这部分代码(设置滚动条):
vscroll = Scrollbar(frame, orient=VERTICAL, command=txtOutput.yview)
txtOutput['yscroll'] = vscroll.set
vscroll.pack(side=RIGHT, fill=Y)
Run Code Online (Sandbox Code Playgroud)
我的滚动条位于所有窗口内,而不位于文本框中:
但是,当然,我希望在文本框小部件(黑色边框)中具有滚动条。如果我使用打包功能到文本框:
txtOutput.pack(fill=BOTH, expand=Y)
Run Code Online (Sandbox Code Playgroud)
文本小部件填充整个窗口...:
我真的不知道如何解决这个问题。任何帮助将不胜感激。谢谢!
编辑:
当然我也可以在Scrollbar中使用place方法,但是我不能更改它们的长度,因为它没有属性长度。
vscroll.place(x=573, y=75)
Run Code Online (Sandbox Code Playgroud)
尽管我很少建议使用place,但是当您利用配置选项时,它功能非常强大。例如,您可以in_用来指定要相对于此小部件放置的小部件。您可以relx用来指定相对的x坐标,也可以relheight用来指定高度。
在您的情况下,您可以尝试执行以下操作:
vscroll.place(in_=txtOutput, relx=1.0, relheight=1.0, bordermode="outside")
Run Code Online (Sandbox Code Playgroud)
如果您想让滚动条像在某些平台上一样(或过去)嵌入到文本小部件中,我建议将文本小部件和滚动条放在框架中。您可以pack用来将这些小部件放在框架中,并继续用于place将组合放置在您想要的任何位置。
例如:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5647 次 |
| 最近记录: |