Josh
Josh

Reputation: 77

How can I ensure that my buttons do not move in tkinter?

Before I press the equals button on my program, I get this:

Before

But after, if my answer is smaller than 0 or greater than 9, this happens:

After

For this same reason I can't use

self.button.config(width=x, height=y)

Without messing up the entire code. Here's a small peice of my code:

import tkinter as tk

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

    def create_widgets(self):
        self.c_button = tk.Button(self)
        self.c_button['text'] = 'C'
        self.c_button.command = self.clear_all
        self.c_button.grid(row=6, column=1)

    def clear_all(self):
        self.a = '0'
        self.math1 = 0
        self.math2 = 0

Is there any way to stop it from moving the other buttons apart?

Upvotes: 1

Views: 549

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33719

You need to configure the grid so that the widget for the result spans multiple columns, using columnspan. You have not shown the part of the code which creates the result, but I suspect you need to do something like this:

    self.result_button.grid(row=0, column=0, columnspan=4)

Upvotes: 3

Related Questions