影响网格布局tkinter的列表框/文本条目

时间:2015-05-04 00:43:36

标签: python tkinter listbox grid-layout

我正在创建一个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 个答案:

答案 0 :(得分:0)

使用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()