1

I was trying to program a kind of calculator in Python using the Tkinter library. My problem is that I watch some pages that says that the way to set de heidth and weidth of a button is using rowconfigure and columnconfigure. The problem is that when I run the script it doesn't work. I don't know what I'm doing bad so pls help me

Here is the code

import tkinter as tk


def insert_number(variable, entry):
    result = variable + entry
    return result


conjunt = ""


class MainWindow(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.grid()

        self.config(bg="blue")
        self.button1 = tk.Button(text="1", command=insert_number(conjunt, "1")).grid(
            column=0, row=0)
        self.button1 = tk.Button(text="2", command=insert_number(conjunt, "2")).grid(
            column=1, row=0)
        self.button1 = tk.Button(text="3", command=insert_number(conjunt, "3")).grid(
            column=2, row=0)
        self.button1 = tk.Button(text="4", command=insert_number(conjunt, "1")).grid(
            column=0, row=1)
        self.button1 = tk.Button(text="5", command=insert_number(conjunt, "1")).grid(
            column=1, row=1)
        self.button1 = tk.Button(text="6", command=insert_number(conjunt, "1")).grid(
            column=2, row=1)
        self.button1 = tk.Button(text="7", command=insert_number(conjunt, "1")).grid(
            column=0, row=2)
        self.button1 = tk.Button(text="8", command=insert_number(conjunt, "1")).grid(
            column=1, row=2)
        self.button1 = tk.Button(text="9", command=insert_number(conjunt, "1")).grid(
            column=2, row=2)
        self.button1 = tk.Button(text="0", command=insert_number(conjunt, "1")).grid(
            column=1, row=3)

        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=1)




    def give_result(self):
        pass


def main():
    root = tk.Tk()
    root.title("Calculadora")

    buttons_frame = MainWindow(root)
    buttons_frame.grid()

    root.mainloop()


if __name__ == "__main__":
    main()
SaturnDev
  • 161
  • 7
  • Does this answer your question? [Tkinter button expand using grid](https://stackoverflow.com/questions/53073534/tkinter-button-expand-using-grid) – stovfl Feb 27 '20 at 22:32
  • Not really. In that question they are working on the Main window, but my problem is on a Frame – SaturnDev Feb 28 '20 at 00:11
  • There is no difference using a `Tk()` window or a `Frame`. You have to allow growing here: `buttons_frame.grid()` – stovfl Feb 28 '20 at 00:13
  • Sorry but I do not understand jajaja. Do you mean that I have to write this?```keyboard.grid(row=0,column=0, sticky=NSEW)```? – SaturnDev Feb 28 '20 at 01:01
  • To change the size of the buttons you can use `padx` and `pady` like this: `Button(root, text='Anything', command=lambda: anything(a, b), padx=50, pady=30)`. Or the second way you can do it with `ipadx` and `ipady` while `grid`ing like this: `tk.Button(root, text='Anything', command=lambda: anything(a, b)).grid(ipadx=50, ipady=30)`. And another thing is that you have to use `lambda` when passing any function with arguments to a `Button` as the `command`. – DaniyalAhmadSE Feb 28 '20 at 13:27
  • @Thebigbosslong ***" Do you mean ...?`keyboard.grid(row=0,column=0, sticky=NSEW)`"***: I mean `buttons_frame.grid(sticky=...)`. You are doing: `root` => `root.Frame` => `Frame`. Means your are using a `Frame` in `Frame`. Therefore you have to allow the second `Frame`, which is your `buttons_frame` to grow using `root.grid_*configure(0, weight=1)` and `.grid(sticky=...`) – stovfl Feb 28 '20 at 14:04

0 Answers0