#๐Ÿ”’ tkinter library, how do i modify a var in a func, if the func that creates the var is below the mo

18 messages ยท Page 1 of 1 (latest)

craggy brambleBOT
#

@undone isle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

undone isle
obsidian granite
#

Your original post was deleted.

undone isle
#

!code

#
def w4cal1(entry3, entry4, outputstr2):
  print("Definition called")
  legit = True
  try:
   no1 = float(entry3.get())
   no2 = float(entry4.get())
  except ValueError:
   outputstr2.set("Error!")
   legit = False
   print(f"Error reached {no1}, {no2}")

  if legit !=False:
    res1 = no1 / 100
    res2 = res1 * no2
    outputstr2.set(f"{no2}% of {no1} is {res2}")
    print("Result reached")

def windo1():
  w4 = tk.Tk()
  w4.title("Calculate a '%' of target value, with 100'%' being defined")
  w4.geometry("800x600")
  w4.resizable(False,False)

  global outputstr2
  outputstr2 = tk.StringVar()
  outputstr2.set("Result")

  label4 = tk.Label(text="Calculate the '%' of a 100% value",master=w4, font="impact 30")
  label5 = tk.Label(text="input 100% value",master=w4, font="impact 25")
  entry3 = tk.Entry(master=w4)
  label6 = tk.Label(text="Input a '%'", master=w4, font="impact 20")
  entry4 = tk.Entry(master=w4)
  button5 = tk.Button(text="Calculate",master=w4, command=lambda: w4cal1(entry3, entry4, outputstr2))
  label7 = tk.Label(text="output", textvariable=outputstr2, font="impact 15", master=w4, wrap=None)

  label4.pack()
  label5.pack(pady=20)
  entry3.pack()
  label6.pack(pady=10)
  entry4.pack()
  button5.pack(pady=20)
  label7.pack(pady=20)

  w4.mainloop()
#

func windo1 creates a label text var

#

then w4cal1 tries to edit it but it cant access it

#

when ran, it prints the "result reached" but outputstr2 doesnt get edited

obsidian granite
#

All of the variables inside of windo1except for outputstr2 are locals that are destroyed when windo1 returns. You need to define them globally instead, or return them from the function.

undone isle
#

global outputstr2
outputstr2 = tk.StringVar()
outputstr2.set("Result")

they are global here

obsidian granite
#

Whoops, misread that part. I thought you were trying to access entry3. I missed that it was a parameter.

undone isle
#

lol, anyway is it solvable easily or is the whole code written in some ancient library?

obsidian granite
#

Not sure unfortunately. This seems to be a Tkinter-specific problem.

undone isle
#

damn, um is there any temp way to replace this so i can continue?

#

or is there any other place you would recommend me to ask?

craggy brambleBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.