使用Python Tkinter和网格布局管理器

时间:2015-10-03 03:39:00

标签: python tkinter

我正在尝试编写一个带有两个并排可滚动列表框的简单应用程序。无论窗口大小如何,我希望它们各自占据窗口的一半。虽然我的窗口可以调整大小,但列表框保持相同的大小,只是在各自的一半水平居中。我做错了什么?

from Tkinter import *
import os
import sys

class ScrollableList(Frame):

    def __init__(self, parent, vscroll=True, hscroll=False):
        Frame.__init__(self, parent)
        self.grid(sticky=NSEW)
        if vscroll:
            self.vScrollbar = Scrollbar(self, orient=VERTICAL)
            self.vScrollbar.grid(row=0, column=1, sticky=N+S)
        if hscroll:
            self.hScrollbar = Scrollbar(self, orient=HORIZONTAL)
            self.hScrollbar.grid(row=1, column=0, sticky=E+W)
        self.listbox = Listbox(self, selectmode=SINGLE)
        self.listbox.grid(row=0, column=0)
        if vscroll:
            self.listbox['yscrollcommand'] = self.vScrollbar.set
            self.vScrollbar['command'] = self.listbox.yview
        if hscroll:
            self.listbox['xscrollcommand'] = self.hScrollbar.set
            self.hScrollbar['command'] = self.listbox.xview
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=0)
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=0)

class Application(Frame):

    @classmethod
    def main(cls):
        NoDefaultRoot()
        root = Tk()
        app = cls(root)
        root.grid_columnconfigure(0, weight=1)
        root.grid_rowconfigure(0, weight=1)
        root.resizable(True, True)
        root.mainloop()

    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.grid(sticky=NSEW)
        options = dict(sticky=NSEW, padx=3, pady=4)
        self.list1 = ScrollableList(self)
        self.list2 = ScrollableList(self)
        self.list1.grid(row=0, column=0, **options)
        self.list2.grid(row=0, column=1, **options)
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

if __name__ == "__main__":
    Application.main()

1 个答案:

答案 0 :(得分:1)

在将实际列表框添加到其父级时,您忘记设置int r = 10, c = 10, i, j; int (*MatrixA)[r]; MatrixA=malloc(c * sizeof(*MatrixA)); int (*MatrixB)[r]; MatrixB=malloc(c * sizeof(*MatrixB)); memcpy(MatrixB,MatrixA,c * sizeof(MatrixA)); for(i=1;i<r+1;i++) { for (j = 1; j < c+1; j++) { MatrixA[i][j]=j; printf("A[%d][%d]= %d\t",i,j,MatrixA[i][j]); } printf("\n"); } printf("\n");printf("\n");printf("\n");printf("\n");printf("\n"); for(i=1;i<r+1;i++) { for (j = 1; j < c+1; j++) { printf("B[%d][%d]= %d\t",i,j,MatrixB[i][j]); } printf("\n"); } 属性,这会导致列表框的边缘“粘贴”#34;到为他们分配的区域的边缘。

sticky
相关问题