无法让此滚动条与Tkinter中的文本小部件一起使用?

时间:2014-06-07 08:32:14

标签: python python-2.7 tkinter

我知道有很多问题,但我需要一些帮助才能让它发挥作用。基本上有一个文本小部件,我希望它在右侧垂直的一侧有一个滚动条。我想要Text小部件中的滚动条,并且它始终可见。到目前为止,这是我的代码。 (BTW由于某种原因,每当我用.pack()运行东西时,python控制台就会冻结?所以用.grid()这样做会很感激,如果不可能的话那就好了):

console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="w")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.pack(side="right", fill="y")

完整代码:

from Tkinter import *
import socket
import tkFont
#User Interface---------------------------------------
root = Tk()
root.title("MY GUI")
root.geometry("372x414+510+175")
root.resizable(width=FALSE, height=FALSE)
'''FONTS------------------------------------------------'''
label_f = tkFont.Font(family="Trebuchet MS", size="12" )
entry_f = tkFont.Font(family ="Courier", size = "12")
'''IP-----------------------------'''
target = Label(root, text="Target IP Adress:",font= label_f).grid(row=0, column=0, sticky='w')
userIP = StringVar()
tar_ent = Entry(root, width = "33", font=entry_f, textvariable = userIP)
tar_ent.grid(row=1, column=0, sticky='w')
'''Port ------------------------------'''
custom = Label(root, text = "Custom port scanner (Enter like 23,80,etc.)", font=label_f).grid(row=2, column=0, sticky='w')
userPort = StringVar()
cus_port = Entry(root, width="33", font=entry_f, textvariable=userPort)
cus_port.grid(row=3, column=0, sticky='w')
'''Results--------------------------'''
space = Label(root, text="")
space.grid(row=5, column=0)
console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="w")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.grid(row=6, column=1, sticky='Ns')#side="right", fill="y")

1 个答案:

答案 0 :(得分:2)

不要为同一个父窗口小部件混合布局(packgridplace)。

替换以下行:

scrollbar.pack(side="right", fill="y")

使用:

scrollbar.grid(row=6, column=1, sticky='NS')

更新根据问题更改:

这里修改了完整的代码:

from Tkinter import *
import socket
import tkFont
#User Interface---------------------------------------
root = Tk()
root.title("MY GUI")
root.geometry("372x414+510+175")
root.resizable(width=FALSE, height=FALSE)
'''FONTS------------------------------------------------'''
label_f = tkFont.Font(family="Trebuchet MS", size="12" )
entry_f = tkFont.Font(family ="Courier", size = "12")
'''IP-----------------------------'''
target = Label(root, text="Target IP Adress:",font= label_f).grid(row=0, column=0, sticky='w')
userIP = StringVar()
tar_ent = Entry(root, width = "33", font=entry_f, textvariable = userIP)
tar_ent.grid(row=1, column=0, sticky='WE', columnspan=2)
'''Port ------------------------------'''
custom = Label(root, text = "Custom port scanner (Enter like 23,80,etc.)", font=label_f).grid(row=2, column=0, sticky='w')
userPort = StringVar()
cus_port = Entry(root, width="33", font=entry_f, textvariable=userPort)
cus_port.grid(row=3, column=0, sticky='WE', columnspan=2)
'''Results--------------------------'''
space = Label(root, text="")
space.grid(row=5, column=0)
console = Text(root, width=35, height=16)
console.grid(row=6, column=0, sticky="WE")

scrollbar = Scrollbar(root, command=console.yview)
console.config(yscrollcommand=scrollbar.set)
scrollbar.grid(row=6, column=1, sticky='Ns')

root.mainloop()

enter image description here