jeudi 1 mai 2014

changer l'étiquette automatiquement en python - Stack Overflow


I am trying to change a label automatically in python, I want it to change every half a second, this is my code for tkinter, the function being called (that is being put into "message") returns a new string every half a second, what am I doing wrong?


import Tkinter as tk

class Application(tk.Frame):
def __init__(self):

self.root = tk.Tk()
self.root.geometry("150x136")

tk.Frame.__init__(self, self.root)
self.create_widgets()

def create_widgets(self):
self.root.bind('<Return>', self.parse)
self.grid()

self.instruction = tk.Label(self, text = "QuickReader")
self.instruction.grid(row = 0, column = 0, columnspan = 4)

self.entry = tk.Entry(self)
self.entry.grid(row = 2, column = 0)

self.submit = tk.Button(self, text="Submit")
self.submit.bind('<Button-1>', self.parse)
self.submit.grid(row = 4, column = 0)

self.words = tk.Label(self, text = "Start")
self.words.grid(row = 5, column = 0, columnspan = 4)


def parse(self, event):
filename = self.entry.get()
message = open_txt(filename)
self.words.set(message)

def start(self):
self.root.mainloop()

Application().start()



To change label text, use one of following:


self.words.config(text=message)

self.words.configure(text=message)

self.words['text'] = message



http://effbot.org/tkinterbook/label.htm



You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:



v = StringVar()
Label(master, textvariable=v).pack()

v.set("New Text!")

So, that should be pretty easy to implement.



I am trying to change a label automatically in python, I want it to change every half a second, this is my code for tkinter, the function being called (that is being put into "message") returns a new string every half a second, what am I doing wrong?


import Tkinter as tk

class Application(tk.Frame):
def __init__(self):

self.root = tk.Tk()
self.root.geometry("150x136")

tk.Frame.__init__(self, self.root)
self.create_widgets()

def create_widgets(self):
self.root.bind('<Return>', self.parse)
self.grid()

self.instruction = tk.Label(self, text = "QuickReader")
self.instruction.grid(row = 0, column = 0, columnspan = 4)

self.entry = tk.Entry(self)
self.entry.grid(row = 2, column = 0)

self.submit = tk.Button(self, text="Submit")
self.submit.bind('<Button-1>', self.parse)
self.submit.grid(row = 4, column = 0)

self.words = tk.Label(self, text = "Start")
self.words.grid(row = 5, column = 0, columnspan = 4)


def parse(self, event):
filename = self.entry.get()
message = open_txt(filename)
self.words.set(message)

def start(self):
self.root.mainloop()

Application().start()


To change label text, use one of following:


self.words.config(text=message)

self.words.configure(text=message)

self.words['text'] = message


http://effbot.org/tkinterbook/label.htm



You can associate a Tkinter variable with a label. When the contents of the variable changes, the label is automatically updated:



v = StringVar()
Label(master, textvariable=v).pack()

v.set("New Text!")

So, that should be pretty easy to implement.


0 commentaires:

Enregistrer un commentaire