我的问题是我不能在不使用grid()的情况下将滚动条放在我的列表框中。除非我创建第二帧,否则我不能使用grid()。我正在努力用一个类来创建第二个框架。下面是我使用该类的代码。我没有尝试将def标签()添加到我的 init 中,因为它会抛出使用grid()的语法。我想使用grid()作为我的def列表框...所以如果我创建第二帧我应该能够使用grid(),如果他们在自己的框架内。我希望。
#!/usr/bin/python
#!/home/lubuntu/subscription_app
from Tkinter import *
from ttk import *
#from subscription_app.enter import *
import tkFileDialog
class start(Frame):
def __init__(self, parent):
self.parent = parent
Frame.__init__(self, parent)
self.initUI()
self.menubar()
self.onExit()
self.statusbar()
self.bottom_button()
self.listbox()
def initUI(self):
self.parent.title("sfgsdfsdsdfsdy")
self.centerWindow()
self.pack()
def menubar(self):
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="???")#command=)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=self.onExit)
def labels(self):
lbl1 = Label(self, text="Members List:")
lbl2 = Label(self, text="Member Information:")
lbl1.grid(row=0, column=0, sticky=W)
lbl2.grid(row=0, column=1, sticky=W)
def listbox(self):
lb = Listbox(self, selectmode=SINGLE)
lb.pack(expand=True, fill=Y)
scrollbar = Scrollbar(self, orient="vertical")
scrollbar.pack(side=RIGHT, fill=Y)
for line in range(100):
lb.insert(END, "This is line number " + str(line))
def statusbar(self):
status = Label(self, text="preparing to do nothing", relief=SUNKEN, anchor=S)
status.pack(expand=TRUE, side=BOTTOM, fill=X)
def bottom_button(self):
saveButton = Button(self, text="Save member details") ##, command=)
saveButton.pack(side=BOTTOM, padx=5, pady=5)
addButton = Button(self, text="Add member")
addButton.pack(side=LEFT)
def onExit(self):
self.quit()
def centerWindow(self):
w = 600
h = 500
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
def main():
root = Tk()
app = start(root)
root.mainloop()
if __name__ == '__main__':
main()
下面是另一批不使用类的代码,所有内容都是手动输入的,这样可以更好地显示应用程序。我宁愿使用我的顶级代码并学习如何正确地做事。
#!/usr/bin/python
#!/home/lubuntu/subscription_app
from Tkinter import *
from ttk import *
#from subscription_app.enter import *
import tkFileDialog
window = Tk()
window.configure(bg='lightgrey')
window.title("sgdfsdfdsfsdy")
## resize screen ##
w = 600
h = 500
sw = window.winfo_screenwidth()
sh = window.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
## text above boxes ##
lbl1 = Label(window, text="Members List:")
lbl2 = Label(window, text="Member Information:")
lbl1.grid(row=0, column=0, sticky=W)
lbl2.grid(row=0, column=1, sticky=W)
## boxes resize with screen ##
frm = Frame(window)
frm.grid(row=1, column=0, sticky=N+S)
window.rowconfigure(1, weight=1)
window.columnconfigure(1, weight=1)
## member list box with scroll ##
scrollbar = Scrollbar(frm, orient="vertical")
scrollbar.pack(side=RIGHT, fill=Y)
listNodes = Listbox(frm, width=20, yscrollcommand=scrollbar.set, font=("Helvetica", 12))
listNodes.pack(expand=True, fill=Y)
scrollbar.config(command=listNodes.yview)
## member details box ##
listSelection = Listbox(window, height=4, font=("Helvetica", 12))
listSelection.grid(row=1, column=1, sticky=E+W+N)
## all buttons ##
saveButton = Button(window, text="Save member details") ##, command=)
saveButton.grid(row=1, column=1, sticky=W)
addButton = Button(window, text="Add member")
addButton.grid(row=2, column=0, sticky=W, pady=10)
deleteButton = Button(window, text="delete member")
deleteButton.grid(row=2, column=0, pady=10)
## status bar ##
status = Label(window, text="preparing to do nothing", relief=SUNKEN, anchor=S)
status.grid(sticky=W+E, columnspan=2)
## menu bar ##
menubar = Menu(window)
window.config(menu=menubar)
fileMenu = Menu(menubar)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="???")#command=)
fileMenu.add_separator()
fileMenu.add_command(label="Exit") #command=window.onExit)
# def onExit(self):
# self.quit()
for x in range(100):
listNodes.insert(END, x)
for x in "ABCD":
listSelection.insert(END, x + ": ?")
window.mainloop()
答案 0 :(得分:0)
我不太清楚问题是什么。如果你想创建一个列表框和滚动条,并将它们放在一个框架中,那么你需要做的就是:创建一个框架并在其中放置一个列表框和滚动条。
def listbox(self):
# create the frame
lbframe = Frame(self)
lbframe.pack(side="left", expand=True, fill=Y)
# create a listbox and scrollbar as children of the frame
lb = Listbox(lbframe, selectmode=SINGLE)
scrollbar = Scrollbar(lbframe, orient="vertical")
# pack the listbox and scrollbar in the frame
lb.pack(side="left", expand=True, fill=BOTH)
scrollbar.pack(side=RIGHT, fill=Y)
for line in range(100):
lb.insert(END, "This is line number " + str(line))
如果您只想将grid
用于列表框和滚动条,那么您当然可以这样做。在这种特定情况下,我不确定它是否比使用pack
提供任何好处。