-1

Trying to establish a GUI whereby the user clicks a button and set information appears in the relevant rows and columns.

Print doesn't achieve the desired outcome and I don't understand GUIs well enough to try other things. I'm a rookie so I'm very lost.

normal = {'Protein' : {'32.50'},
          'Carbohydrates' : {'60'},
          'Fat' : {'40.86'}}


from tkinter import *
from tkinter import ttk

root = Tk()
root.title('Diet Information')

frame = ttk.Frame(root, padding='150 150 300 300')
frame.grid(column=0, row=0, sticky=(N, W, E, S))
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

d_label = ttk.Label(frame, text='Selected diet: ')
d_label.grid(column=1, row=3, sticky=W)
p_label = ttk.Label(frame, text='Protein (g): ')
p_label.grid(column=1, row=4, sticky=W)
c_label = ttk.Label(frame, text='Carbohydrates (g): ')
c_label.grid(column=1, row=5, sticky=W)
f_label = ttk.Label(frame, text='Fat (g): ')
f_label.grid(column=1, row=6, sticky=W)
k_label = ttk.Label(frame, text='Kilojoules (kJ): ')
k_label.grid(column=1, row=7, sticky=W)
s_label = ttk.Label(frame, text='Select a diet to display:')
s_label.grid(column=3, row=8, sticky=W)

n_button = ttk.Button(frame, text='Normal', command=print(normal))
n_button.grid(column=3, row=9, sticky=W)

I wish for the 'normal' button to return the information required for each label. I will then use this method for multiple buttons.

Jordan Gamson
  • 33
  • 1
  • 4
  • Instead of printing, you have to create a label and set its text. – Aran-Fey May 30 '19 at 05:55
  • Read [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – Henry Yik May 30 '19 at 06:13

1 Answers1

0

In tkinter, instead of print statements there are labels. Just like if you wanted to print a variable print(fred) you set a Label to a variable. Instead of having ttk.Label(frame, text), have ttk.Label(frame, textvariable=variable). In order to use a Label with variables correctly, you need to set up the variable correctly. Say I still want to display variable. First, before putting it in the Label, it needs to be set up. You need to set it up to take in the type of data you are going to give it. Some examples of this are StringVar(), IntVar(), BooleanVar() and DoubleVar(). They basically just state that the data will be a string (StringVar), Integer(IntVar), Boolean(BooleanVar), or a float(DoubleVar). Say you have data for 15 grams of Carbs. That is an integer, so you would use IntVar(). The way to set it all up would look like this.

variable = IntVar()
variable.set(carbs)
ttk.Label(frame, textvariable=variable).pack()

In this example, carbs would be your float value the user input, and variable is the actual variable you put in the label. Hope this helps, happy coding!

Quick edit: Buttons work best when you assign a function to them, so I would suggest setting a function to use the values in normal (if that's the data you want to use) and take that and assign it to variables that you put into your Labels.