Changing text of label does not work - python

I have a problem with the following python script. Later, it will catch the data from a barcode scanner and display the text as a label. But whenever the text should be changed from the label (highlighted line), the program crashes. I am an absolute beginner Python and can not explain that. I comment out the line, the program works.
from Tkinter import *
import pyHook
class Application(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Sc4nn0r")
self.variable = "Start Variable"
self.master.geometry("363x200")
self.master.resizable(0,0)
self.master.rowconfigure( 0, weight = 1)
self.master.columnconfigure( 0, weight = 1 )
self.grid( sticky = W+E+N+S )
self.label4String = StringVar()
self.label4 = Label(self, textvariable=self.label4String)
self.label4.grid( row = 2, column = 1, columnspan = 2, sticky = W+E+N+S)
self.label4String.set("Variable1")
self.string = ''
hook = pyHook.HookManager()
hook.KeyDown = self.read
hook.HookKeyboard()
def read(self, event):
print(event.Ascii);
if event.Ascii != 13:
self.string = self.string + chr(event.Ascii)
else:
self.post(self.string.strip(' \0'))
self.string = ''
return True
def post(self,string):
self.label4String.set(string) # THIS LINE I Mean ##########
print(string)
def main():
Application().mainloop()
if __name__ == '__main__':
main()
I hope it can someone help me.

I would suggest getting rid of StringVar in its entirety. Instead, use self.label4 = Label(self, text = "Variable1"). Then, whenever you wish to change the label, you can use self.label4.config(text = string).

Related

Getting and calculating stuff through tkinter widets

I was wondering how to calculate stuff using tkinter buttons. I'm making a simple program to calculate seconds to hours:minutes:seconds. The user inputs an integer using the entry widget on the seconds box and when they press calculate, they get the result via the converted time line. I'm confused on how to start calculating it. I know you get the integer via .get, but I'm stuck on how to do that and calculate it in a h:m:s format. This is my code so far.
import tkinter
from tkinter import *
class TimeConverterUI():
def __init__(self):
self.root_window = Tk()
self.root_window.geometry('400x150')
self.root_window.title('Seconds Converter')
self.text()
self.calculate_button()
self.quit_button()
self.root_window.wait_window()
def text(self):
row_label = tkinter.Label(
master = self.root_window, text = 'Seconds: ')
row_label.grid( row = 0, column = 0, columnspan=2, padx=10, pady=10,
sticky = tkinter.W)
secondsEntry = Entry(master = self.root_window)
secondsEntry.grid(row = 0, column = 1)
row_label = tkinter.Label(
master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)
def calculate_button(self):
quit = Button(self.root_window, text = "Calculate", command = self.calculate)
quit.grid(row = 3, column = 0, columnspan = 3, pady=20,
sticky = tkinter.W)
def calculate(self):
pass
def quit_button(self):
quit = Button(self.root_window, text = "Quit", command = self.quit)
quit.grid(row = 3, column = 3, columnspan = 3, pady=20,
sticky = tkinter.E)
def quit(self) -> bool:
self.root_window.destroy()
return True
if __name__ == '__main__':
convert=TimeConverterUI()
First break this code below into 2 lines if you ever want to use row_label later because this will return NoneType. You should define it first then use .grid on it (just like your button).
row_label = tkinter.Label(
master = self.root_window, text = 'Converted Time(H:M:S): ').grid(row=1)
Now you can create another label to show the result. Remember to put self. before its name so you can use it in the calculate function. Also change secondsEntry to self.secondsEntry for the same reason.Now you just use int(self.secondsEntry.get()) in that function and do the required calculations. Then set the result to that result label with .configure(text=str(result))

Python Tkinter. How to store entry via button in label

I really need help with some code. I don't expect you to write it for me, since it is a school project, but I am just really lost and need help.
The code I am writing is some sort of production system.
It doesn't need to actually be able to send a task anywhere, since this is just an imagined scenario.
The code has to consist of three files: data.py, model.py and gui.py.
Gui can access the two other files
Data can only access model
Model can't access either of the other two.
My teacher had written some of the code witch I have continued on. Some of the text is in danish, but most comments are in English.
The code is as follows.
data.py
from model import *
class Data(object):
def __init__(self):
self.units = []
self.finished_tasks = []
def __str__(self):
result = "These tasks have been finished: "
for i in self.finished_tasks:
result += str(i)
return result
def task_done(self, unit):
done_task = unit.task_done()
if done_task != None:
#TODO: add to list of finished tasks
pass
def add_task(self, name, amount, unit):
s = Springroll_task(name, amount)
unit.add_to_queue(s)
def read_from_database(self):#doesn't actually read from db..
self.units.append(Production_unit("maskine1"))
self.units.append(Production_unit("maskine2"))
self.add_task("Miniruller", 100, self.units[0])
self.add_task("Maxiruller", 200, self.units[0])
self.add_task("HowIRoll", 3000, self.units[0])
self.add_task("RulleMarie", 40, self.units[1])
self.add_task("Rullesten", 500, self.units[1])
self.add_task("Toiletpapirsruller", 6000, self.units[1])
model.py
class Springroll_task(object):
def __init__(self, name, amount):
self.name = name
self.amount = amount
def __str__(self):
return self.name + " " + str(self.amount)
class Production_unit(object):
def __init__(self, amount={}, name={},):
#name of the production unit
self.name = name
self.amount = amount
#the current task
self.current_task = None
#the tasks in the queue
self.springroll_queue = []
#the size of the queue
self.queue_size = 0
def __str__(self):
#TODO
return self.name + " " + str(self.amount)
def add_to_queue(self, task={}):
if self.current_task == None:
self.current_task = task
else:
self.springroll_queue.append(task)
self.queue_size += 1
#remember to update queue_size
pass
def task_done(self):
#TODO: remember the old current task.
#Set the current task to be the first in the queue (and remove from queue)
# - if there is a task in the queue.
#return the old current task.
#remember to update queue_size
self.queue_size -= 1
pass
gui.py
from tkinter import *
from model import *
from data import Data
class Application(Frame):
def __init__(self, master, unit):
self.mod = Production_unit()
super(Application, self).__init__(master)
self.grid()
self.unit = unit
self.create_widgets()
def create_widgets(self):
self.unit_name_lbl = Label(self, text = self.unit.name)
self.unit_name_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
self.cur_prod_lbl = Label(self, text = "produktion nu: ")
self.cur_prod_lbl.grid(row = 1, column = 0, columnspan = 2, sticky = W)
self.prod_lbl = Label(self, text = "produkt")
self.prod_lbl.grid(row = 2, column = 0, sticky = W)
self.amount_lbl = Label(self, text = "antal")
self.amount_lbl.grid(row = 2, column = 1, sticky = W)
#Label for production now
self.amount1_lbl = Label(self, text = " ", bg ="red")
self.amount1_lbl.grid(row = 3, column = 0, sticky = W)
self.amount2_lbl = Label(self, text = " ", bg ="red")
self.amount2_lbl.grid(row = 3, column = 1, sticky = W)
#Button for task finished
self.finished_but = Button(self, text = "Opgave afsluttet", bg ="pink", command=self.mod.task_done)
self.finished_but.grid(row = 3, column = 2, sticky = W)
#Label for queue
self.queue_lbl = Label(self, text = "Kø")
self.queue_lbl.grid(row = 4, column = 0, sticky = W)
#Label for production queue
for i in range(0,3):
self.name_lbl =Label(self, text = self.mod.springroll_queue, bg="red", width= 6)
self.name_lbl.grid(row = 5+i, sticky = W)
for j in range(0,3):
self.qt_lbl =Label(self, text = self.mod.springroll_queue, bg="red", width= 4)
self.qt_lbl.grid(row = 5+j, column = 1)
self.new_lbl = Label(self, text = "Ny")
self.new_lbl.grid(row = 10, column = 0, sticky = W)
#Entry for entries
self.eq1_ent = Entry(self, text = "", width=6)
self.entry_name = self.eq1_ent.get()
self.eq1_ent.grid(row = 11, sticky = W)
self.ea1_ent = Entry(self, text = "", width=4)
self.ea1_ent.grid(row = 11, column = 1, sticky = W)
#Button for add to queue
self.add_but = Button(self, text = "Tilføj til kø", bg ="pink", command=self.mod.add_to_queue(self.ea1_ent.get()))
self.add_but.grid(row = 11, column = 2, sticky = W)
def done(self):
d.task_done(self.unit)
self.redraw()
def add(self):
n = "Nyt navn" #read from gui
a = "Nyt antal" #read from gui
d.add_task(n, a, unit)
self.redraw()
def redraw(self):
#TODO
pass
# main
root = Tk()
root.title("Daloon")
root.geometry("300x300")
d = Data()
d.read_from_database()
p = d.units[0]
app = Application(root, p)
root.mainloop()
So it currently looks like this:
What I need to be able to do is to take an input in the bottom two entry widgets and put them in one of the 4 label widgets above, beginning from the top and then in the queue afterwards, this should happen when I press the button add_but, which seems to be gone currently.
After that I need the task stored in the data file when the "Opgave afsluttet" button is pressed.
I really hope someone is able to help me!
I edited it with some suggestions, and am calling the right self.eq1_ent.get() now, I think. I dont get any error any longer, now I just don't really know how to make it do what I want.
Edit 2: I am slowly getting some stuff, so i have made changes in the model.py and gui.py...
It looks like this now:
self.eq1 is not defined. you have self.q1_lbl and self.eq1_ent.
To access the label use self.q1_lbl.
To be able to set text to your label create them as following:
self.var = StringVar()
self.unit_name_lbl = Label(self, textvariable=self.var)
For example, from redraw() you can set 'text' to self.unit_name_lbl like this : self.var.set('text').
Check if you missed self in d.add_task(n, a, unit)
When you do command=mod.add_to_queue(self.ea1_ent.get()) the mod.add_to_queue function will be called directly, if you want to pass argument to this function when user press the button, you can use lambda:
command=lambda: mod.add_to_queue(self.ea1_ent.get)

How to get variables from a function to another function?

I am trying to create a simple Tkinter user registration app and have run into a problem. I want to get the input from an Entry to another function.
My code as of now:
from Tkinter import *
def addUsr():
username = sv.get() #here
password = sv1.get() #here
page = open("Users.txt", 'r+')
contents = page.read()
page.write("--->")
page.write("\n")
page.write("username: " + username)
page.write("\n")
page.write("password: " + password)
page.write("\n")
page.write("<---")
page.write("\n")
page.close()
print contents
def reg():
usrs = Tk()
usrs.title("Text")
usrs.geometry('450x300+200+200')
sv = StringVar()
sv1 = StringVar()
ent1 = Entry(usrs, textvariable=sv).pack()
ent2 = Entry(uses, textvariable=sv1).pack
button1 = Button(usrs, text="submit", command=addUsr).pack(side='bottom', padx=15, pady=15)
usrs.mainloop()
I want to get the sv and sv1 into the addUsr function, but this code returns the error message:
username = sv.get()
NameError: global name 'sv' is not defined
When the code is built up so that the second function is not a function this code works. I just want to find a way to get the same result but using this structure. How do you suppose I do this?
I think this is a structural problem. You normally would use a class to create the application and put everything under that class. Then accessing functions and variables from anywhere is straightforward. I have created a simple example because I do not have time to rewrite all of your code!
from Tkinter import *
class Application(Frame): # create a class to hold the application within
def __init__(self, master): # boot it up
""" Initialize the frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.var1 = "var1"
self.var2 = "var2"
def create_widgets(self): # create the GUI interface
self.lbl = Label(self, text = "xxxx")
self.lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W, pady = 4)
self.input = Text(self, width = 35, height = 5, wrap = WORD)
self.input.grid(row = 1, column = 0, columnspan = 2, sticky = W)
self.submit_button = Button(self, text = "submit", command = self.functionX)
self.submit_button.grid(row = 2, column = 0, columnspan = 2, sticky = W)
def functionX(self):
print "do some stuff"
self.var3 = "yet another variable" # create a variable in one function for access in another
self.functionY() # call one function from another function
def functionY(self):
print "do something else"
print self.var1 # access some class variables
print self.var2
print self.var3
root = Tk()
root.title("xxxxxxxxxxxx")
root.geometry("330x310")
app = Application(root)
I hope this helps, it is pretty self explanatory when you run the code and read it to understand what is going on. (by the way I ended up using PyQt/Pyside with QtDesigner instead of Tkinter, you may want to check that out if you don't know about it already).

Increment a character's ascii code

In the code below, where I am learning Tkinter, I would like it so that a button press or key press changes the text from "A" to "B". As you can see, it starts at "A" ... and then cheats by explicitly changing it to "B". I want to access the first position in the StringVar (probably index 0), and then increment it as a character based on its ascii code, then convert it back to the StringVar. Is this possible? Is there a simpler way than using StringVar here?
from Tkinter import *
from tkMessageBox import *
print "this is a test"
class Demo(Frame):
def __init__(self):
self.createGUI()
print "init"
#self.__mainWindow = Tk()
def destroy(self):
print "destroy"
def createGUI(self):
Frame.__init__(self)
self.pack(expand = YES, fill = BOTH)
self.master.title("Demo")
self.trackLabel = StringVar()
self.trackLabel.set("A")
self.trackDisplay = Label(self, font = "Courier 14", textvariable = self.trackLabel, bg = "black", fg = "green")
self.trackDisplay.grid(sticky = W+E+N+S)
self.button1 = Button(self, text = "Move Forward", width = 20, command = self.bpress)
self.button1.grid(row = 2, column = 0, sticky = W+E+N+S)
self.bind_all('<Key>', self.print_contents)
def bpress(self):
self.trackLabel.set("B") # I want to do this so it cycles through the alphabet!
print "ow"
#INSTEAD I WANT TO MAKE TRACKLABEL ASCII CHAR + 1 HERE
def print_contents(self, event):
print "testing here...", self.trackLabel.get()
#INSTEAD I WANT THIS TO MAKE TRACKLABEL ASCII CHAR + 1 HERE BY KEY, OR ABOVE BY BUTTON PRESS
# run the program
def main():
tts = Demo()
tts.mainloop()
if __name__ == "__main__":
main()
You can get a character's ascii code using ord and convert a code back into a character using chr.
code = ord(self.trackLabel.get())
self.trackLabel.set(chr(code+1))

python: Taking an Tkinter entry value

I've been trying to get an entry value (the S1 in the code) to set itself as a value (STR in the _attributes dictionary), and I just can't get it to work. I want to make this an eventual toploop, but am going a step at a time on this, as I'm new to programming in general. Am I going about this the right way, or should I just have a button that, when pressed, does a lookup on the entry value at that time and goes with it, instead? I've gone through several tutorials and lessons I've found online for Tkinter, but still seem to be miles away from being able to make anything work the way I expect it to.
#! usr/bin/python27
from Tkinter import *
class Character:
def __init__(self, **kvargs):
self._attributes = kvargs
def set_attributes(self, key, value):
self._attributes[key] = value
return
def get_attributes(self, key):
return self._attributes.get(key, None)
def attrInput(stat, x, y):
"""Creates a label for entry box"""
L = Label(B,
width = 5,
relief = RIDGE,
anchor = E,
text = stat).grid(row = x,
column = y)
B = ""
def main():
Person = Character()
B = Tk()
S1 = Entry(B, width = 3)
S1.grid(row = 0, column = 1)
S1.bind("<Key>", Person.set_attributes('STR', S1.get()) )
attrInput("Str: ", 0, 0)
Button(B, text='Quit', command=B.destroy).grid(row=3, column=0, sticky=W, pady=4)
B.mainloop()
print Person.__dict__
if __name__ == '__main__': main()
new code (seems to be working, I'm getting what I want out of it, at least). I'll have to modify it slightly to make it a toploop, but here's the foundation
class Character:
def __init__(self, **kvargs):
self._attribute = kvargs
def set_attribute(self, key, value):
self._attribute[key] = value
return
def get_attribute(self, key):
return self._attribute.get(key, None)
class attrAsk:
def __init__(self, master, Char, attrName, Row, Column):
self.Char = Char
self.attrName = attrName
attrInput(attrName+":", Row, Column)
self.e = Entry(master, width = 3)
self.e.grid(row = Row, column = Column+1)
self.e.bind("<KeyRelease>", self.set_attr)
def set_attr(self, event):
self.Char.set_attribute(self.attrName, self.e.get())
def attrInput(stat, x, y):
"""Creates a label for entry box"""
L = Label(box,
width = 5,
relief = RIDGE,
anchor = E,
text = stat).grid(row = x,
column = y)
Person= Character()
box = Tk()
STRENT = attrAsk(box, Person, "STR", 0, 0)
DEXENT = attrAsk(box, Person, "DEX", 1, 0)
CONENT = attrAsk(box, Person, "CON", 2, 0)
INTENT = attrAsk(box, Person, "INT", 3, 0)
WISENT = attrAsk(box, Person, "WIS", 4, 0)
CHAENT = attrAsk(box, Person, "CHA", 5, 0)
Button(box,
text='Continue',
command=box.destroy).grid(columnspan = 2,
row=8,
column=0,
sticky=W,
pady=4)
box.mainloop()
print Person.__dict__
Change the line:
S1.bind("<Key>", Person.set_attributes('STR', S1.get()) )
to something like:
def key_pressed(event):
Person.set_attributes('STR', S1.get())
S1.bind("<KeyRelease>", key_pressed)
There are two reasons the original code doesn't work:
bind takes a function as its second argument- that function is then called when the event occurs. The expression Person.set_attributes('STR', S1.get()) as you use it, however, just happens immediately. You need to put that expression into a function so that it happens only when the key is pressed.
<Key> means the event occurs when the key is first pressed, but you would rather it happen when the key is released (and therefore the new character has been added). You thus want to use <KeyRelease>.
One other note: it would be a good idea to organize all your functionality, especially the callback methods, into a class. For example:
class Window(object):
def __init__(self):
self.person = Character()
self.B = Tk()
self.S1 = Entry(B, width = 3)
self.S1.grid(row = 0, column = 1)
self.S1.bind("<KeyRelease>", self.key_pressed)
attrInput("Str: ", 0, 0)
self.button = Button(B, text='Quit', command=self.B.destroy).grid(row=3, column=0, sticky=W, pady=4)
self.B.mainloop()
print self.person.__dict__
def key_pressed(self, event):
self.person.set_attributes('STR', self.S1.get())
def main():
w = Window()
if __name__ == '__main__': main()
The benefit of this organization might not be immediately apparent, but it becomes very useful once you have a large number of callback methods and are keeping track of a large number of widgets.
In response to your comment, you can create both the Entry and the Label objects in a for loop, each on its own row. The key_pressed method can then learn the field and the input text from the event object that gets passed to it, as seen here (try it):
class Window(object):
def __init__(self):
self.person = Character()
self.B = Tk()
self.fields = ["STR", "DEX", "CON", "INT", "WIS", "CHA"]
self.inputs = {}
for i, f in enumerate(self.fields):
self.inputs[f] = Entry(B, width = 3)
self.inputs[f].grid(row=i, column=1)
self.inputs[f].bind("<KeyRelease>", self.key_pressed)
attrInput(f + ":", i, 0)
self.button = Button(B, text='Quit', command=self.B.destroy).grid(row=7, column=0, sticky=W, pady=4)
self.B.mainloop()
print self.person.__dict__
def key_pressed(self, event):
field = self.fields[int(event.widget.grid_info()["row"])]
self.person.set_attributes(field, event.widget.get())

Categories