Tkinter:粘得好不合适

时间:2017-07-11 15:28:02

标签: python python-3.x user-interface tkinter grid

我的问题类似于this question。简而言之,我遇到了粘滞问题。

我没有权重的代码如下所示:

from tkinter import *; 


class calculator:

    #def update(self, 

    def __init__(self, window):
        """
        Constructor method.
        """

        self.toCompute = []; self.In = StringVar(); self.Out = StringVar();
        self.In.set("Hello"); self.Out.set("Hi there!");

        # Window title
        window.title("Logic Calculator");

        # The set of 5 frames.
        ioFrame = Frame(window, relief=GROOVE, borderwidth=3); 
        ioFrame.grid(row=0, sticky=N+E+S+W, columnspan=2); 
        nwFrame = Frame(window); nwFrame.grid(row=1,column=0); 
        neFrame = Frame(window); neFrame.grid(row=1,column=1); 
        swFrame = Frame(window); swFrame.grid(row=2,column=0); 
        seFrame = Frame(window); seFrame.grid(row=2,column=1); 

        # Top 2 rows: the IO portion
        Label(ioFrame, textvariable=self.In, relief=SUNKEN).grid(row=0, sticky=N+W);
        Label(ioFrame, textvariable=self.Out).grid(row=1, sticky=S+E,columnspan=2);

        # Top left 2x2 Frame: [ ( | ) ][ T | F ]
        brlButton = Button(nwFrame, text='(', height=2, width=10).grid(row=0,column=0);
        brrButton = Button(nwFrame, text=')', height=2, width=10).grid(row=0,column=1);
        truButton = Button(nwFrame, text='T', height=2, width=10).grid(row=1,column=0);
        falButton = Button(nwFrame, text='F', height=2, width=10).grid(row=1,column=1);

        # Top right 2x2 Frame: [ AND | OOR ][ NND | NOR ]
        andButton = Button(neFrame, text='and', height=2, width=10).grid(row=0,column=0);
        oorButton = Button(neFrame, text='oor', height=2, width=10).grid(row=0,column=1);
        nndButton = Button(neFrame, text='nnd', height=2, width=10).grid(row=1,column=0);
        norButton = Button(neFrame, text='nor', height=2, width=10).grid(row=1,column=1);

        # Bottom left 2x2 Frame: [ SSO | IIF ][ NSO | NIF ]
        andButton = Button(swFrame, text='sso', height=2, width=10).grid(row=0,column=0);
        oorButton = Button(swFrame, text='iif', height=2, width=10).grid(row=0,column=1);
        nndButton = Button(swFrame, text='nso', height=2, width=10).grid(row=1,column=0);
        norButton = Button(swFrame, text='nif', height=2, width=10).grid(row=1,column=1);

        # Bottom right 2x2 Frame: [ EEQ | NEG ][ NEQ | === ]
        eeqButton = Button(seFrame, text='eeq', height=2, width=10).grid(row=0,column=0);
        negButton = Button(seFrame, text='neg', height=2, width=10).grid(row=0,column=1);
        neqButton = Button(seFrame, text='neq', height=2, width=10).grid(row=1,column=0);
        comButton = Button(seFrame, text='=', height=2, width=10).grid(row=1,column=1);


if __name__ == "__main__": # Only runs program if this specfic file is opened.

    window = Tk(); # The window
    calculator(window);
    window.mainloop(); 

但是,这并不像我想要的那样。我希望输入行(iFrame中的内容)将SUNKEN浮雕拉伸到整个行/行。此外,我希望输入保持左对齐(因此sticky=N+W,尽管N看起来多余,但无论如何)。另外,我希望输出是右对齐的。

基本上,最终的图片应该是this

感谢您的帮助。

PS。我知道我不需要Python中的分号。我只是喜欢他们哈哈。

1 个答案:

答案 0 :(得分:0)

我没有看到输入行的条目小部件,因此我将使用ioFrame内的内容。

我假设您希望带有SUNKEN浮雕设置的标签在其框架内伸展。

实现此目的的方法是通过执行以下操作来向窗口小部件首先添加权重1:

ioFrame.columnconfigure(0, weight = 1)

然后你需要改变:

Label(ioFrame, textvariable=self.In, relief=SUNKEN).grid(row=0, sticky=N+W)

要:

Label(ioFrame, textvariable=self.In, relief=SUNKEN, anchor = W).grid(row=0, sticky=N+W+E)

通过将anchor = W添加到小部件的创建中,我们将标签的textvariable与左侧对齐。并且通过更改粘贴来说sticky = N+W+E,你告诉python拉伸小部件以适合左右两侧的单元格。

还有一件事。您可能想要更改分配NSEW的方式。这些常量仅在您使用*或单独导入时才有效。如果您通过使用引号更改导入方式,则可以使用不同的方式编写它们以防止方向代码出现任何问题。 IE:sitcky = "nsew"

以下是使用所需更改修改的代码。我仍然认为在这里使用;毫无意义。

from tkinter import *; 


class calculator:

    def __init__(self, window):

        self.toCompute = []; self.In = StringVar(); self.Out = StringVar();
        self.In.set("Hello"); self.Out.set("Hi there!");
        window.title("Logic Calculator")

        ioFrame = Frame(window, relief=GROOVE, borderwidth=3); 
        ioFrame.grid(row=0, sticky=N+E+S+W, columnspan=2)
        ioFrame.columnconfigure(0, weight = 1) 
        nwFrame = Frame(window); nwFrame.grid(row=1,column=0); 
        neFrame = Frame(window); neFrame.grid(row=1,column=1); 
        swFrame = Frame(window); swFrame.grid(row=2,column=0); 
        seFrame = Frame(window); seFrame.grid(row=2,column=1); 

        Label(ioFrame, textvariable=self.In, relief=SUNKEN, anchor = W).grid(row=0, sticky=N+W+E);
        Label(ioFrame, textvariable=self.Out).grid(row=1, sticky=S+E,columnspan=2);

        brlButton = Button(nwFrame, text='(', height=2, width=10).grid(row=0,column=0);
        brrButton = Button(nwFrame, text=')', height=2, width=10).grid(row=0,column=1);
        truButton = Button(nwFrame, text='T', height=2, width=10).grid(row=1,column=0);
        falButton = Button(nwFrame, text='F', height=2, width=10).grid(row=1,column=1);

        andButton = Button(neFrame, text='and', height=2, width=10).grid(row=0,column=0);
        oorButton = Button(neFrame, text='oor', height=2, width=10).grid(row=0,column=1);
        nndButton = Button(neFrame, text='nnd', height=2, width=10).grid(row=1,column=0);
        norButton = Button(neFrame, text='nor', height=2, width=10).grid(row=1,column=1);

        andButton = Button(swFrame, text='sso', height=2, width=10).grid(row=0,column=0);
        oorButton = Button(swFrame, text='iif', height=2, width=10).grid(row=0,column=1);
        nndButton = Button(swFrame, text='nso', height=2, width=10).grid(row=1,column=0);
        norButton = Button(swFrame, text='nif', height=2, width=10).grid(row=1,column=1);

        eeqButton = Button(seFrame, text='eeq', height=2, width=10).grid(row=0,column=0);
        negButton = Button(seFrame, text='neg', height=2, width=10).grid(row=0,column=1);
        neqButton = Button(seFrame, text='neq', height=2, width=10).grid(row=1,column=0);
        comButton = Button(seFrame, text='=', height=2, width=10).grid(row=1,column=1);

if __name__ == "__main__": 
    window = Tk();
    calculator(window);
    window.mainloop(); 
相关问题