0

I have 3 buttons I want to make it so when you press a button it sets the variable to a value. press 1st button sets x to 1 2nd set y to 2 press 3rd add x and y together should return with 3 but it returns with 0.

I tried to make a 4th value where when it is set to 1 it would add both together. me and a friend tried making x and y at the beginning and integer int(x)= x

here is the code

import tkinter

window_main = tkinter.Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

i=0

x=0

y=0

o = 0

p = 0

x = int(x)

y = int(y)

o = int(o)

p = int(p)

i=int(i)

def submitFunction() :
  x = 1
  print(x)

if o == 1:
  x = 1


def a2():
  y = 2
  print(y)

if p == 1:
  y = 2

def equ():
  i = int(x + y)
  return(i)
  print(i)

button_submit = tkinter.Button(window_main, text ="set x to 1", command=submitFunction).grid(column = 1, row = 1)

button = tkinter.Button(window_main, text ="set y to 2", command=a2).grid(column = 2, row = 1)

button = tkinter.Button(window_main, text = "solve", command = equ). grid(column = 3, row = 1)

window_main.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
Ignite58
  • 1
  • 1
  • How exactly are you checking the values of "variables"? Do you mean that you want **text to appear in the GUI**? Because neither `print` nor `return` will have **anything to do with** what appears inside the GUI window. – Karl Knechtel Feb 14 '23 at 15:02

1 Answers1

0

Given your code if you look carefully there are two Variables by the name 'x' and 'y' and whenever you press the button it does execute the button however the variables in inside those function are treated as local variables although you have defined them outside the function which should make them global variables. But in the case of python global variables can be accessed inside the function but cannot be modified.

So in this case in order to treat the 'x' and 'y' with global scope so they can be modified or changed inside the function which reflects it variables outside, we use global Keyword inside function in order to make their scope global.

I have modified your code a bit.

from tkinter import *
import tkinter

window_main = Tk(className='Tkinter - TutorialKart', )
window_main.geometry("400x200")

i=0
x=0
y=0
o = 0
p = 0

def submitFunction() :
    global x
    x = 1
    print(x)
    if o == 1:
        x = 1

def a2():
    global y
    y = 2
    print(y)

    if p == 1:
        y = 2

def equ():
    i = x + y
    print(i)
    return (i)


button_submit = tkinter.Button(window_main, text ="set x to 1",         command=submitFunction).grid(column = 1, row = 1)

button = tkinter.Button(window_main, text ="set y to 2", command=a2).grid(column = 2, row = 1)

button = tkinter.Button(window_main, text = "solve", command = equ). grid(column = 3, row = 1)

window_main.mainloop()

If you want to read more about calling functions in tkinter without parenthesis click this link the person has explained it perfectly. Also check variable scoping in Python link

You might get more clarity on understanding your problem after reading.