Home python I can not transfer the value of ComboBox and Entry to the...

I can not transfer the value of ComboBox and Entry to the function. Python Tkinter GUI

Author

Date

Category

in Python, especially in Tkinter I am a beginner, I am writing a calorie calculator as semester. There was a problem of transmitting the values ​​of dishes_box.get () and grams.get () from a child window, which is caused by the function, to the add_a_dish () function, which is the command for the button. I tried to use and lambda expressions and partial function, but the result did not achieve.

def choose_dish (name, massa):
  Name = dishes_box.get ()
  Base_of_Dishes = Open ('dishes.txt', Encoding = 'UTF-8')
  For Line in Base_of_Dishes:
    Line = line.split (',')
    IF Line [0] == STR (Name):
      Bilok_Now = Float (Line [1]) * (Float (Massa) / 100)
      Jir_Now = Float (Line [2]) * (Float (Massa) / 100)
      Vyg_Now = Float (Line [3]) * (Float (Massa) / 100)
      Calories_Now = Float (Line [4]) * (Float (Massa) / 100)
      Break
      Base_of_dishes.Close ()
Def add_a_dish ():
  a = []
  Base_of_Dishes = Open ('dishes.txt', Encoding = 'UTF-8')
  For Line in Base_of_Dishes:
    Line = line.split (',')
    A.APPEND (str (Line [0]))
  Base_of_dishes.Close ()
  grams = tk.intvar ()
  Window = TK.Toplevel (Master = Calculator_Window)
  Window.Geometry ('300x200')
  Window.title ('Dalid Strut ")
  TK.Label (Window, Text = 'Ober_nuvous List Introducts ї, font =' arial 8 '). Pack ()
  dishes_box = tk.ttk.comBoBox (Window, Values ​​= A)
  dishes_box.pack ()
  x = dishes_box.get ()
  TK.Label (Window, Text = 'Vaga in gramm:'). Pack ()
  TK.Entry (Window, TextVariable = Grams) .Pack ()
  TK.Button (Window, Text = 'OK', Command = Partial (choose_dish, x, grams.get ())). Pack (Pady = 10)

Thank you in advance)


Answer 1, Authority 100%

Instead of Partial you need to use lambda, because You .get Methods are called immediately when you create a button, and you need to be called only after pressing the button:

tk.button (window, text = 'ok', Command = Lambda: Choose_Dish (dishes_box.get (), grams.get ())). Pack (Pady = 10)

Well and in choose_dish Remove the name = dishes_box.get () string, because The name is already transmitted through the Name parameter.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions