Python GUI只显示一个标签而不是两个

时间:2016-05-29 18:35:14

标签: python user-interface

所以我正在制作游戏,我想知道为什么行数/列数显示一个而不是同时显示两者。当我评论一个,另一个显示,反之亦然,而不是两个显示。

class OthelloGUI():

    def __init__(self):

        self._root_window = tkinter.Tk()

        self._root_window.title('Othello')

        self.read_row()

        self.read_column()


    def read_row(self) -> int:


        self.row_text =tkinter.StringVar()

        self.row_text.set('Num of rows:')

        row_label = tkinter.Label(
                master = self._root_window, textvariable = self.row_text,
                background = 'yellow', height = 1, width = 10, font = DEFAULT_FONT)

        row_label.grid(row=1, column = 0, padx = 10, pady=10, sticky = tkinter.W+tkinter.N)

        return self.row.get()


    def read_column(self) -> int:

        self.column_text =tkinter.StringVar()

        self.column_text.set('Num of columns:')

        column_label = tkinter.Label(
                master = self._root_window, textvariable = self.column_text,
                background = 'yellow', height = 1, width = 13, font = DEFAULT_FONT)

        column_label.grid(row=1, column = 0, padx = 10, pady=50, sticky = tkinter.W+tkinter.N)

        return self.column.get()

1 个答案:

答案 0 :(得分:1)

您使用相同的坐标调用grid

row_label.grid(row=1, column = 0, padx = 10, pady=10, sticky = tkinter.W+tkinter.N)

column_label.grid(row=1, column = 0, padx = 10, pady=50, sticky = tkinter.W+tkinter.N)

当您在(1,0)处都网格时,第二个将覆盖第一个。而是使用不同的行/列参数:

row_label.grid(row=1, column = 0, padx = 10, pady=10, sticky = tkinter.W+tkinter.N)

column_label.grid(row=2, column = 0, padx = 10, pady=50, sticky = tkinter.W+tkinter.N)

当然,在您的界面中将行/列设置为您想要的任何内容。