-1

I'm trying to create a calculator type of layout but the numbers mess up when I add the output field.

import Tkinter as tk

window = tk.Tk()
window.title("BUTTON CALCULATOR")
window.geometry("400x400")


#----OutPut----
answer_output = tk.Text(master = window, height = 1, width = 20)
answer_output.grid(column = 0, row = 0)

#----BUTTONS----

buttonOne = tk.Button(text = "1")
buttonOne.grid(column = 0, row = 1)

numberTwo = tk.Button(text = "2")
numberTwo.grid(column = 1, row = 1)

numberThree = tk.Button(text = "3")
numberThree.grid(column = 2, row = 1)

window.mainloop()

How do I make 1, 2, and 3 sit next to each other on the left wide of the window?

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
DeDude
  • 1
  • 1

1 Answers1

2

The main problem is the missing columnspan for your textbox. The next thing you will probably want is sticky on the buttons so they stretch in the columns.

Take a look at the below example and let me know if you have any questions.

import tkinter as tk

window = tk.Tk()
window.title("BUTTON CALCULATOR")
window.geometry("400x400")

answer_output = tk.Text(master=window, height=1, width=20)
answer_output.grid(column=0, row=0, columnspan=3)

buttonOne = tk.Button(text="1")
buttonOne.grid(column=0, row=1, sticky="ew")

numberTwo = tk.Button(text="2")
numberTwo.grid(column=1, row=1, sticky="ew")

numberThree = tk.Button(text="3")
numberThree.grid(column=2, row=1, sticky="ew")

window.mainloop()

Results:

enter image description here

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79