lundi 18 mai 2015

Unable to display the line in label

import tkinter
import tkinter.messagebox
import json
import random


class ProgramGUI:

    def __init__(self):
        try:
            file = open("lines.txt", "r")
            data = json.load(file)
            file.close()
        except FileNotFoundError:
            print("Missing/Invalid File")
            self.main.destroy()
        except ValueError:
            print("Missing/Invalid File")
            self.main.destroy()

        self.main = tkinter.Tk()
        self.main.title('QuizBox')
        self.main.geometry('350x150')
        self.main.resizable(width=True, height=True)

        self.main.configure(bg='White', cursor='circle')

        self.main.attributes('-alpha', 1)
        self.main.attributes('-topmost', True)

        self.timer = tkinter.IntVar()
        self.timer.set(11)

        self.var = tkinter.StringVar()
        self.label = tkinter.Label(self.main, textvariable=self.var)
        self.label.pack()

        self.buttonClicked = False 

        self.label = tkinter.Label(self.main, textvariable=self.timer)
        self.label.pack()

        self.button = tkinter.Button(self.main, text='Click me!', command=self.click)
        self.button.pack(fill='x')

        self.updateTimer() # call updateTimer as the program begins
        tkinter.mainloop()

    def loadLine(self):
        for i, que in enumerate(data):
            self.var.set = random.choice(que["line"])

    def click(self): 
        # this method is called when the button is clicked
        # it sets the buttonClicked attribute to True, and shows a messagebox
        self.buttonClicked = True
        tkinter.messagebox.showinfo('Results', 'You clicked it in time!')



    def updateTimer(self):
        # this method is called when the program is first launched.
        # if the button has not been clicked, it reduces the timer by 1 and
        # then sets the function to be called again in 1 second.

        if not self.buttonClicked: # if buttonClicked is False

            self.timer.set(self.timer.get() - 1) # decrease counter by 1

            if self.timer.get() == 0: # if counter is at 0, disable the button            
                self.button.configure(state='disabled', text='Too slow!')

            else: # otherwise, call the function again in a second
                self.main.after(1000, self.updateTimer)

gui = ProgramGUI()

I am new to python and trying to work tkinter. I am able to display the time and the button but unable to display the line(from the file) in label above the timer and how can I randomly display the line(from the table) when the user clicks.




Aucun commentaire:

Enregistrer un commentaire