影響網格布局tkinter的列表框/文本條目

[英]Listbox/text entry affecting grid layout tkinter


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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/3df4bf61742d60da5c30f188f8fb028a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com