-4

Objective: click on plus button and a plus sign appears in the entry box I have a very simple code here:

basic=tk.Tk()
basic_label=tk.Label('Welcome to Basic Math section. \n \n Note: Please enter white spaces between objects, \n e.g. 5 + 6 \n NOT 5+6')
entry=tk.Entry()
userInput=str(entry.get())
input1, op, input2=UserInput.split(' ')

#casting inputs
input1=int(input1)
input2=int(input2)
plus=tk.Button(text='+', command=??)

What do I put in the command?

Husun
  • 29
  • 1
  • 7

2 Answers2

0

Try this. I have fixed your GUI as well as commenting out some erroneous code.

import tkinter as tk
basic=tk.Tk()
basic_label=tk.Label(basic,text='Welcome to Basic Math section. \n \n Note: Please enter white spaces between objects, \n e.g. 5 + 6 \n NOT 5+6')
basic_label.grid()
entry=tk.Entry(basic)
entry.grid()
userInput=str(entry.get())
#input1, op, input2=userInput.split(' ')

#casting inputs
#input1=int(input1)
#input2=int(input2)
plus=tk.Button(basic,text='+',command=lambda:entry.insert(tk.END,"+"))
plus.grid()
basic.mainloop ()
Minion Jim
  • 1,239
  • 13
  • 30
  • Thanks Minion for answering the question but who has this benefited, has the OP really learnt anything? Also add explanations to your solutions. – Moller Rodrigues Feb 13 '18 at 19:19
  • Probably not but you have to start somewhere (it took ages for me to learn Tkinter originally). Also, thanks for the reminder about comments (I do have quite bad coding habits...) – Minion Jim Feb 13 '18 at 19:20
0

Your code for creating a label omits the frame argument, causing the text to not display on the frame:

label = tk.Label("Welcome to Basic Math section...")

Instead, make sure that you pass root (your frame) to the Label constructor:

label = tk.Label(root, text = "", <any other options>)
label.grid(row=x, column=x)
  • root - the parent you assigning this widget to
  • text - set the text of the label
  • <any other options> - these can be label dimensions, features check link for more
  • .grid() - adds widget to frame in a grid like layout where you can define x for rows and columns

Check out this link for more information regarding tkinter: http://effbot.org/tkinterbook/

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
Moller Rodrigues
  • 780
  • 5
  • 16