I'm making a setlist creator that will basically allow someone to copy a title from the library into a setlist. When I put everything in the grid, for some reason the text area and listbox are bumping the buttons and entry box oddly even though I've checked the rows and columns to make sure they're not overlapping. Should I be using frames or something? If this has to do with frames, how would I do that?
我正在创建一个setlist创建器,它基本上允许某人将库中的标题复制到setlist中。当我把所有东西放在网格中时,由于某种原因,即使我检查了行和列以确保它们没有重叠,文本区域和列表框也会奇怪地碰到按钮和输入框。我应该使用框架还是什么?如果这与帧有关,我该怎么做?
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.SetList_Label = Label(self, text = "Set List")
self.SetList_Label.grid(row = 0, column = 0, padx = 5, pady = 5, sticky = W)
self.SetList_Box = Text(width = 15, height = 10)
self.SetList_Box.grid(row = 1, column = 0, rowspan = 9, columnspan = 4, padx = 5, sticky = W)
self.empty = Label(self, text = " ")
self.empty.grid(row = 0, column = 1, columnspan = 4)
self.Library_Label = Label(self, text = "Library")
self.Library_Label.grid(row = 0, column = 5, padx = 5, sticky = W)
self.Library_List = Listbox(self)
self.Library_List.insert(1, "Dooley")
self.Library_List.insert(2, "Icky Wicky")
self.Library_List.insert(3, "Pig in a Pen")
self.Library_List.insert(4, "In the Highways")
self.Library_List.insert(5, "Bile 'Em Cabbage")
self.Library_List.insert(6, "I'll Fly Away")
self.Library_List.grid(row = 1, column = 5, sticky = W)
self.addto_setlist = Button(self, text = "Add To Setlist", command = self.get_title)
self.addto_setlist.grid(row = 12, column = 0, padx = 5, sticky = W)
self.addto_library = Button(self, text = "Add To Library", command = self.add_item)
self.addto_library.grid(row = 12, column = 6, sticky = W)
self.addtitle_box = Entry()
self.addtitle_box.grid(row = 12, column = 5, sticky = W)
self.delete_button = Button(self, text = "Delete Title", command = self.delete_item)
self.delete_button.grid(row = 12, column = 7, padx = 5, sticky = W)
def add_item(self):
self.Library_List.insert (END, self.addtitle_box.get())
def delete_item(self):
try:
index = self.Library_List.curselection()[0]
self.Library_List.delete(index)
except IndexError:
pass
def get_title(self):
index = self.Library_List.curselection()[0]
seltext = self.Library_List.get(index)
self.SetList_Box.insert(END, "\n" + seltext)
root = Tk()
root.title("SetList Creator")
root.geometry("500x600")
app = Application (root)
root.mainloop()
1 个解决方案
#1
Use the width= parameter to size the widgets (same for all widgets but will change with different fonts) http://effbot.org/tkinterbook/listbox.htm http://effbot.org/tkinterbook/label.htm Like your books BTW.
使用width =参数来调整小部件的大小(所有小部件都相同,但会使用不同的字体进行更改)http://effbot.org/tkinterbook/listbox.htm http://effbot.org/tkinterbook/label.htm喜欢你的书BTW。
import Tkinter as tk
master=tk.Tk()
tk.Label(master, text="default width",
bg="lightblue").grid()
tk.Label(master, text="width=25", width=25,
bg="yellow").grid(row=1)
tk.Label(master, text="width=25", width=25, bg="yellow",
font=("Helvetica", 15)).grid(row=2)
tk.Label(master, text="width=50", width=50,
bg="orange").grid(row=3)
tk.Button(master, text="Quit", command=master.quit).grid(row=4)
master.mainloop()
#1
Use the width= parameter to size the widgets (same for all widgets but will change with different fonts) http://effbot.org/tkinterbook/listbox.htm http://effbot.org/tkinterbook/label.htm Like your books BTW.
使用width =参数来调整小部件的大小(所有小部件都相同,但会使用不同的字体进行更改)http://effbot.org/tkinterbook/listbox.htm http://effbot.org/tkinterbook/label.htm喜欢你的书BTW。
import Tkinter as tk
master=tk.Tk()
tk.Label(master, text="default width",
bg="lightblue").grid()
tk.Label(master, text="width=25", width=25,
bg="yellow").grid(row=1)
tk.Label(master, text="width=25", width=25, bg="yellow",
font=("Helvetica", 15)).grid(row=2)
tk.Label(master, text="width=50", width=50,
bg="orange").grid(row=3)
tk.Button(master, text="Quit", command=master.quit).grid(row=4)
master.mainloop()