mercredi 26 octobre 2016

Random selection of variables from a dictionary and print on a GUI in python

I am stuck in a code of making a "Food Quiz" GUI on python 3 using the tkinter and init method. As I am a newbie I am having difficulties in this program. The aim is to make a GUI program which will take various keys from a dictionary and set them randomly on the GUI. Please refer to the attached image. The Big Mac and Whopper are the keys (name) and they have to appear randomly on the buttons and then in the body of the GUI as shown. Currently, the data is in the .txt file and we have to import it as well. The other key attribute to be selected is from a list of variables namely cholesterol, fat, protein and etc. as shown in the image. Please help me with the random selection of these after importing them from the .txt file and then printing it on the GUI.

from tkinter import Tk, LEFT, RIGHT, BOTH, RAISED, END
from tkinter import * 
from tkinter.ttk import Frame, Button, Style
import tkinter.messagebox
from tkinter import Label
import random
import json
class foodquiz(Frame):  
    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.label = Label(parent, text="Which has one more....\n Cholestrol")
        self.label.place(x = 50, y = 70)
        self.initUI()
    def initUI(self):
        self.parent.title("Food Quiz")
        self.style = Style()
        self.style.theme_use("default")
        frame = Frame(self, relief=RAISED, borderwidth=1)
        frame.pack(fill=BOTH, expand=True)
        self.pack(fill=BOTH, expand=True)
        def leftoption():
            tkinter.messagebox.showinfo("Correct Option!", "You got it right.")
        closeButton = Button(self, text="Whopper", command = leftoption)
        closeButton.place(x = 165, y = 154)
        def rightoption():
            tkinter.messagebox.showinfo("Incorrect!", "You got it wrong.")
        okButton = Button(self, text="Big Mac", command = rightoption)
        okButton.place(x = 0, y = 154)
        def middleoption():
            tkinter.messagebox.showinfo("Incorrect!", "You got it wrong.")
        midButton = Button(self, text = "Roughly Equal", command = rightoption)
        midButton.place(x = 71.5, y = 154)
def main():
    root = Tk()
    root.geometry("230x180+300+300")
    app = foodquiz(root)
    root.mainloop()  
if __name__ == '__main__':
    main()

The random varibles must not change until the user enters the answer. Dictionary in .txt file:

[
 {
  "carbohydrates": 45,
  "calories": 540,
  "sodium": 1040,
  "fat": 29,
  "protein": 25,
  "name": "Big Mac",
  "cholesterol": 75
 },
 {
  "carbohydrates": 53,
  "calories": 760,
  "sodium": 1410,
  "fat": 47,
  "protein": 33,
  "name": "Whopper",
  "cholesterol": 100
 },
 {
 "carbohydrates": 85,
 "fat": 5,
 "sodium": 45,
 "calories": 50,
 "protein": 50,
 "name": "Icecream'",
 "cholesterol": 96
 } 
]

Ideal output of the GUI and random variables:

enter image description here




Aucun commentaire:

Enregistrer un commentaire