简体   繁体   中英

How can I make multiple rows of buttons in tkinter?

I've been trying to create a simple calculator in python using the tkinter module.

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()
        self.a = ''
        self.math1 = 0
        self.math2 = 0

    def create_widgets(self):
        self.quit_button = tk.Button(self, text='Q', fg='red', command=self.master.destroy)
        self.quit_button.pack(side='left')

        self.c_button = tk.Button(self)
        self.c_button['text'] = 'C'
        self.c_button.command = self.clear_all
        self.c_button.pack(side='left')

        self.one_button = tk.Button(self)
        self.one_button['text'] = 1
        self.one_button['command'] = self.add_1_to_str
        self.one_button.pack(side='left')

        self.two_button = tk.Button(self)
        self.two_button['text'] = 2
        self.two_button['command'] = self.add_2_to_str
        self.two_button.pack(side='left')

        self.three_button = tk.Button(self)
        self.three_button['text'] = 3
        self.three_button['command'] = self.add_3_to_str
        self.three_button.pack(side='left')

        self.four_button = tk.Button(self)
        self.four_button['text'] = 4
        self.four_button['command'] = self.add_4_to_str
        self.four_button.pack(side='left')

        self.five_button = tk.Button(self)
        self.five_button['text'] = 5
        self.five_button['command'] = self.add_5_to_str
        self.five_button.pack(side='left')

        self.six_button = tk.Button(self)
        self.six_button['text'] = 6
        self.six_button['command'] = self.add_6_to_str
        self.six_button.pack(side='left')

        self.seven_button = tk.Button(self)
        self.seven_button['text'] = 7
        self.seven_button['command'] = self.add_7_to_str
        self.seven_button.pack(side='left')

        self.eight_button = tk.Button(self)
        self.eight_button['text'] = 8
        self.eight_button['command'] = self.add_8_to_str
        self.eight_button.pack(side='left')

        self.nine_button = tk.Button(self)
        self.nine_button['text'] = 9
        self.nine_button['command'] = self.add_9_to_str
        self.nine_button.pack(side='left')

        self.zero_button = tk.Button(self)
        self.zero_button['text'] = 0
        self.zero_button['command'] = self.add_0_to_str
        self.zero_button.pack(side='left')

        self.equal_button = tk.Button(self)
        self.equal_button['text'] = '='
        self.equal_button['command'] = self.equal
        self.equal_button.pack(side='left')

        self.plus_button = tk.Button(self)
        self.plus_button['text'] = '+'
        self.plus_button['command'] = self.plus
        self.plus_button.pack(side='left')

        self.subtract_button = tk.Button(self)
        self.subtract_button['text'] = '-'
        self.subtract_button['command'] = self.subtract
        self.subtract_button.pack(side='left')

        self.multiply_button = tk.Button(self)
        self.multiply_button['text'] = 'X'
        self.multiply_button['command'] = self.multiply
        self.multiply_button.pack(side='left')

        self.divide_button = tk.Button(self)
        self.divide_button['text'] = '/'
        self.divide_button['command'] = self.multiply
        self.divide_button.pack(side='left')

    def plus(self):
        self.operation = '+'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def subtract(self):
        self.operation = '-'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def multiply(self):
        self.operation = '*'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def divide(self):
        self.operation = '/'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def equal(self):
        self.math2 = self.a
        self.math1 = int(self.math1)
        self.math2 = int(self.math2)
        if self.operation == '+':
            self.total = self.math1 + self.math2
        elif self.operation == '-':
            self.total = self.math1 - self.math2
        elif self.operation == '*':
            self.total = self.math1 * self.math2
        elif self.operation == '/':
            self.total = self.math1 / self.math2
        self.math1 = str(self.math1)
        self.math2 = str(self.math2)
        self.total = str(self.total)
        print(self.math1 + self.operation + self.math2 + '=' + self.total)

    def add_1_to_str(self):
        self.a = self.a + '1'

    def add_2_to_str(self):
        self.a = self.a + '2'

    def add_3_to_str(self):
        self.a = self.a + '3'

    def add_4_to_str(self):
        self.a = self.a + '4'

    def add_5_to_str(self):
        self.a = self.a + '5'

    def add_6_to_str(self):
        self.a = self.a + '6'

    def add_7_to_str(self):
        self.a = self.a + '7'

    def add_8_to_str(self):
        self.a = self.a + '8'

    def add_9_to_str(self):
        self.a = self.a + '9'

    def add_0_to_str(self):
        self.a = self.a + '0'

    def clear_all(self):
        self.a = '0'
        print(self.a)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

It works just fine, but I'm wondering how to make it look like this:

1 2 3 +
4 5 6 -
7 8 9 *
|C| 0 /
|||Q||| (|||Q||| means that the Q button will be that large, across the bottom)

Instead of this: QC 1 2 3 4 5 6 7 8 9 0 = + - * /

I'll be adding decimals into the code later on, so I'll also need a . button to the right of the C .

I really have no clue about how to do it, the only think that I've tried is self.button.pack(side='top_left') , but I get an error saying it can only be top, bottom, left or right.

Try this:

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()
        self.a = ''
        self.math1 = 0
        self.math2 = 0

    def create_widgets(self):
        self.quit_button = tk.Button(self, text='Q', fg='red', command=self.master.destroy)
        self.quit_button.grid(row=4, column=0, columnspan=4, sticky="we")

        self.c_button = tk.Button(self)
        self.c_button['text'] = 'C'
        self.c_button.command = self.clear_all
        self.c_button.grid(row=3, column=0)

        self.one_button = tk.Button(self)
        self.one_button['text'] = 1
        self.one_button['command'] = self.add_1_to_str
        self.one_button.grid(row=0, column=0)

        self.two_button = tk.Button(self)
        self.two_button['text'] = 2
        self.two_button['command'] = self.add_2_to_str
        self.two_button.grid(row=0, column=1)

        self.three_button = tk.Button(self)
        self.three_button['text'] = 3
        self.three_button['command'] = self.add_3_to_str
        self.three_button.grid(row=0, column=2)

        self.four_button = tk.Button(self)
        self.four_button['text'] = 4
        self.four_button['command'] = self.add_4_to_str
        self.four_button.grid(row=1, column=0)

        self.five_button = tk.Button(self)
        self.five_button['text'] = 5
        self.five_button['command'] = self.add_5_to_str
        self.five_button.grid(row=1, column=1)

        self.six_button = tk.Button(self)
        self.six_button['text'] = 6
        self.six_button['command'] = self.add_6_to_str
        self.six_button.grid(row=1, column=2)

        self.seven_button = tk.Button(self)
        self.seven_button['text'] = 7
        self.seven_button['command'] = self.add_7_to_str
        self.seven_button.grid(row=2, column=0)

        self.eight_button = tk.Button(self)
        self.eight_button['text'] = 8
        self.eight_button['command'] = self.add_8_to_str
        self.eight_button.grid(row=2, column=1)

        self.nine_button = tk.Button(self)
        self.nine_button['text'] = 9
        self.nine_button['command'] = self.add_9_to_str
        self.nine_button.grid(row=2, column=2)

        self.zero_button = tk.Button(self)
        self.zero_button['text'] = 0
        self.zero_button['command'] = self.add_0_to_str
        self.zero_button.grid(row=3, column=2)

        self.equal_button = tk.Button(self)
        self.equal_button['text'] = '='
        self.equal_button['command'] = self.equal
        self.equal_button.grid(row=3, column=1)

        self.plus_button = tk.Button(self)
        self.plus_button['text'] = '+'
        self.plus_button['command'] = self.plus
        self.plus_button.grid(row=0, column=3)

        self.subtract_button = tk.Button(self)
        self.subtract_button['text'] = '-'
        self.subtract_button['command'] = self.subtract
        self.subtract_button.grid(row=1, column=3)

        self.multiply_button = tk.Button(self)
        self.multiply_button['text'] = 'X'
        self.multiply_button['command'] = self.multiply
        self.multiply_button.grid(row=2, column=3)

        self.divide_button = tk.Button(self)
        self.divide_button['text'] = '/'
        self.divide_button['command'] = self.multiply
        self.divide_button.grid(row=3, column=3)

    def plus(self):
        self.operation = '+'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def subtract(self):
        self.operation = '-'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def multiply(self):
        self.operation = '*'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def divide(self):
        self.operation = '/'
        self.a = int(self.a)
        self.math1 = self.a
        self.a = str(self.a)
        self.a = ''

    def equal(self):
        self.math2 = self.a
        self.math1 = int(self.math1)
        self.math2 = int(self.math2)
        if self.operation == '+':
            self.total = self.math1 + self.math2
        elif self.operation == '-':
            self.total = self.math1 - self.math2
        elif self.operation == '*':
            self.total = self.math1 * self.math2
        elif self.operation == '/':
            self.total = self.math1 / self.math2
        self.math1 = str(self.math1)
        self.math2 = str(self.math2)
        self.total = str(self.total)
        print(self.math1 + self.operation + self.math2 + '=' + self.total)

    def add_1_to_str(self):
        self.a = self.a + '1'

    def add_2_to_str(self):
        self.a = self.a + '2'

    def add_3_to_str(self):
        self.a = self.a + '3'

    def add_4_to_str(self):
        self.a = self.a + '4'

    def add_5_to_str(self):
        self.a = self.a + '5'

    def add_6_to_str(self):
        self.a = self.a + '6'

    def add_7_to_str(self):
        self.a = self.a + '7'

    def add_8_to_str(self):
        self.a = self.a + '8'

    def add_9_to_str(self):
        self.a = self.a + '9'

    def add_0_to_str(self):
        self.a = self.a + '0'

    def clear_all(self):
        self.a = '0'
        print(self.a)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM