Add tkinter's intvar to an integer - python

I'm having some trouble adding a value taken from an Entry box and adding it to an existing number. In this case, I want the value of the "change speed" box to be added to the robots current speed. When run, my code produces an error:
TypeError: unsupported operand type(s) for +=: 'int' and 'IntVar'.
Below is the code that produces the entry box:
change_speed_entry = ttk.Entry(main_frame, width=5) # Entry box for linear speed
change_speed_entry.grid()
data = tkinter.IntVar()
change_speed_entry['textvariable'] = data
And next is where I try to manipulate the result. This is a method within a class. All other methods of the class work correctly:
def changeSpeed(self, delta_speed):
self.speed += delta_speed

You need to first invoke the .get method of IntVar:
def changeSpeed(self, delta_speed):
self.speed += delta_speed.get()
which returns the variable's value as an integer.
Since I don't have your full code, I wrote a small script to demonstrate:
from Tkinter import Entry, IntVar, Tk
root = Tk()
data = IntVar()
entry = Entry(textvariable=data)
entry.grid()
def click(event):
# Get the number, add 1 to it, and then print it
print(data.get() + 1)
# Bind the entrybox to the Return key
entry.bind("<Return>", click)
root.mainloop()
When you run the script, a small window appears that has an entrybox. When you type a number in that entrybox and then click Return, the script gets the number stored in data (which will be the number you typed in), adds 1 to it, and then prints it on the screen.

You didn't show the code defining .speed or delta_speed, so I'm guessing here. Try:
self.speed += delta_speed.get()
^^^^^^
If delta_speed is an IntVar, .get() will retrieve its value as a Python int.

Related

How does stringvar work with textvariable?

I am writing a chess program and I can't get the pieces to output since I changed the text from being defined with text to text variable and now I can't get string working to output the pieces.
I define the buttons in a for loop and it outputs nothing in the squares. The code is
def drawboard(self):
x=0
y=0
for column in range(self.n):
self.changecolours()
x=x+1
y=0
for row in range(self.n):
y=y+1
colour = self.colours[self.colourindex]
pos=(x,9-y)
buttons=(tk.Button(self.boardframe, padx=10, textvariable=lambda position=pos: self.placepieces(position), borderwidth=0, bg=colour, relief="solid", font=self.piecefont, command=lambda position=pos: self.movepiece(position) ))
buttons.grid(column=(x-1), row=(y-1), sticky="W"+"E"+"N"+"S" )
self.changecolours()
#classmethod
def placepieces(cls, position):
black=Black()
white=White()
squareposition=position
icon=tk.Stringvar("")
if squareposition in white.position.values():
for key in white.position:
value=white.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(white.pawntype[key])
elif key=="king":
icon.set(white.PIECES.get("KING"))
elif key=="queen":
icon.set(white.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(white.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(white.PIECES.get("KNIGHT"))
else:
icon.set(white.PIECES.get("ROOK"))
else:
pass
elif squareposition in black.position.values():
for key in black.position:
value=black.position.get(key)
if value==squareposition:
if key.endswith("pawn"):
icon.set(black.pawntype.get(key))
elif key=="king":
icon.set(black.PIECES.get("KING"))
elif key=="queen":
icon.set(black.PIECES.get("QUEEN"))
elif key.endswith("bishop"):
icon.set(black.PIECES.get("BISHOP"))
elif key.endswith("knight"):
icon.set(black.PIECES.get("KNIGHT"))
else:
icon.set(black.PIECES.get("ROOK"))
break
else:
pass
else:
icon.set("")
return icon
How does stringvar work with textvariable?
In the case of a Button, you can configure the text to be displayed in one of two ways: with a hard-coded string (eg: text='click me') or with the textvariable attribute.
The textvariable attribute must be set to an instance of one of the special tkinter variable objects StringVar, IntVar, DoubleVar or BooleanVar (eg: var=tk.StringVar(); Button(..., textvariable=var)). When configured with an instance of one of these variables, the text on the button will display whatever the value of the variable is.
Internally, when you specify a textvariable, tkinter will take the string representation of that object and create a tcl variable in the embedded interpreter. So, while it may see to accept a standard variable, command, or even a lambda, it ultimately ends up creating an internal tcl variable with that name, and associates that internal variable with the underlying tcl widget. If you do not use one of the special variables, it is going to be difficult for you to get or set the value of that variable.
For example, consider this set of commands:
var = tk.StringVar(value="hello")
tk.Button(root, textvariable=var)
When that code is run, the text that appears on the button will be hello. If you change the variable at any time (eg: var.set("goodbye")), the button will automatically be changed to show the new value.

Python Tkinter entry not loading content correct

i am facing a problem. I'm runnig this code.
import tkinter as tk
root = tk.Tk()
def check():
if len(e.get().split("a")) > 1:
print("contains a")
e = tk.Entry(frame1)
e.grid(row=4,column=1,columnspan=2,padx = (10,10), pady=(5,10), sticky="w e")
e.bind("<Key>",check)
when i type "a" to the entry I wont get nothing printed. I'll get the result by tiping a second character. I think that it happens because the function gets executed before the content has actualy changed. I tried to add a timer on the beginning of the function but it does nothing.
I want get the result by entering the first "a". What should I do?
I think that it happens because the function gets executed before the content has actualy changed.
You're right. If you want the callback to be able to see the character you just typed, you should create a StringVar and bind to that instead of binding to a "<Key>" event on the widget.
import tkinter as tk
frame1 = tk.Tk()
def check(*args):
if "a" in s.get():
print("contains a")
s = tk.StringVar()
e = tk.Entry(frame1, textvariable=s)
s.trace("w", check)
e.grid(row=4,column=1,columnspan=2,padx = (10,10), pady=(5,10), sticky="w e")
frame1.mainloop()

How Do I assign a function to a button in Python/TKinter?

I am fairly new to TKinter and have been trying to convert my normal Python code into a GUI (TKinter Code)! I have been working on this code and so far, I have got the basic layout made but I am having problems coding buttons and using entries. You will most likely find a lot of errors in my code so be warned! :D
I have got an entry at the top of the window and I want the user to input a number into the entry and then I want to use the text inputted in the entry in some code (btn1()). I also want the user to press a button and then the button to run some code with labels underneath where the buttons are showing the results of the code (the labels in the btn1() function).
First, I want the user to input a number into the entry. Then, I want the user to click on a button which is underneath the entry. Finally, I want the results of the code behind the button, to be show underneath the buttons (in labels!).
Here is my code:
from tkinter import *
class window_design:
def __init__(self):
root=Tk()
root.title("Bag Weight")
root.geometry("500x700")
root.wm_iconbitmap('favicon.ico')
image=PhotoImage(file="Weight Program.png")
imagelabel=Label(root,image=image)
imagelabel.pack()
weightentrylabel=Label(root,text="Enter Weight!")
weightentrylabel.pack()
self.string=StringVar()
weightentry=Entry(root,textvariable=self.string)
weightentry.pack()
menutext=Label(root,text="What coin are you using?")
menutext.pack(side=LEFT)
values=['1p','2p','5p','10p','20p','50p','£1','£2','Exit']
def btn1(self,btn1code):
p1=3.56
p1should=356
if (self.string.get()) > p1should:
weightdif=(self.string.get())-p1should
coins=weightdif/p1
labeldif=Label(text=weightdif)
labelcoins=Label(text=coins)
elif (self.string.get()) < p1should:
weightdif=p1should-(self.string.get())
coins=weightdif/p1
labeldif=Label(text=weightdif)
labelcoins=Label(text=coins)
button1=Button(root,text="1p",command=btn1)
button1.pack(side=LEFT)
root.mainloop()
window_design()
I am currently getting this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\cjay2\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
TypeError: btn1() missing 2 required positional arguments: 'self' and 'btn1code'
You should use self.btn1 ( btn1 is a class method) in button1=Button(root,text="1p",command=btn1).
btn1() is called with one argument and it needs two arguments, set a default value to btn1code or removeit (if you do not use it).
When you call get() method on StringVar() it will return a string so you need to convert before comparing with integers.
To show result in label use a self.result = StringVar() then call self.result.set(a_string).
Check the following code:
from tkinter import *
class window_design:
def __init__(self):
root=Tk()
root.title("Bag Weight")
#root.geometry("500x700")
root.wm_iconbitmap('favicon.ico')
image=PhotoImage(file="Weight Program.png")
imagelabel=Label(root,image=image)
imagelabel.pack()
weightentrylabel=Label(root,text="Enter Weight!")
weightentrylabel.pack()
self.string=StringVar()
weightentry=Entry(root,textvariable=self.string)
weightentry.pack()
menutext=Label(root,text="What coin are you using?")
#menutext.pack(side=LEFT)
menutext.pack()
values=['1p','2p','5p','10p','20p','50p','£1','£2','Exit']
button1=Button(root,text="1p",command=self.btn1)
#button1.pack(side=LEFT)
button1.pack()
#--------------------------------------------------
self.result=StringVar()
resultlabel=Label(root, textvariable = self.result)
resultlabel.pack()
#--------------------------------------------------
root.mainloop()
#-------------------------------------
def btn1(self):
p1=3.56
p1should=356
if not self.string.get(): return
value = int(self.string.get())
if value > p1should:
weightdif = value - p1should
coins=weightdif/p1
elif value < p1should:
weightdif=p1should - value
coins=weightdif/p1
self.result.set(coins)
#-----------------------------------
window_design()
You defined btn1() as needing an argument besides self, but Tkinter is calling it without one. It looks like you are not even using btn1code, so you can change your function definition to def btn1(self):
you can declare btn1code as member variable,and remove it from your function definition, or if you want call function with arguments from the Button; use lambda function like this :
button1=Button(root,text="1p",command=lambda: btn1(btn1code))

Tkinter "entry" object not updating textvariable variable (Python 3.4)

I am trying to write a simple Python program that will allow a user to input an IP address in decimal, or dotted-decimal format, then convert it to the opposite format and display it in the same entry box (ie, if they enter a decimal IP address, they can click a button and their input will be replaced with the dotted-decimal equivalent).
The problem I'm having is with pulling the data out of the entry box, then putting the new data back into the entry box. I've written an example with just the GUI code, and none of my other conversion logic, to simplify the problem:
import tkinter as tk
root = tk.Tk()
root.title("Test")
win1 = tk.Frame(root)
win1.grid()
x = tk.StringVar()
y = tk.StringVar()
xBox = tk.Entry(win1)
xBox.grid(row = 0, column = 0)
xBox.textvariable = x
yBox = tk.Entry(win1)
yBox.grid(row = 1, column = 0)
yBox.textvariable = y
button = tk.Button(win1,text = "Calculate", command = lambda: copyVal())
button.grid(row = 2, column = 0)
def copyVal():
print("x: " + x.get())
print("y: " + y.get())
xVal = x.get()
print("xval: " + xVal)
y.set(xVal)
root.update_idletasks()
root.mainloop()
Here's what I expect to happen with this code:
The value entered in the top box should be stored in StringVar x.
Clicking the "Calculate" button should run the copyVal() function:
copyVal() gets the value of StringVar x and stores it as xVal.
copyVal() sets the value of StringVar y to match xVal.
The text in the bottom box should now match the text in the top box.
Instead, it does not retrieve the value of StringVar x, so there's nothing to set StringVar y to.
I've tried the following variations:
Using xVal = xBox.get() instead of xVal = x.get(): this retrieves the contents of the top entry box, and sets the value of StringVar y to match it, but the bottom entry box does not change.
Using command = copyVal() instead of command = lambda: copyVal(): the copyVal function executes immediately upon program execution, rather than when the button is pressed.
Moving the copyVal function outside the root mainloop: raises a NameError exception when the button is pressed (copyVal is seen as not defined).
Moving root.update_idletasks() outside the copyVal function has no effect.
I've looked around for solutions to this issue, but no matter how many people I find who are experiencing similar problems, none of their fixes seem to resolve the issue for me (I usually see them told to use StringVar() to get/set values). I am completely new to working with Tkinter, so I'm sure this is something really basic that I'm overlooking, and I appreciate any advice anyone can offer.
Python objects often allow you to add attributes to them arbitrarily:
>>> class Foo:
... pass
...
>>> foo = Foo()
>>> foo.a = 1 # No error. It makes a new attribute.
>>> foo.a
1
>>>
>>> def foo():
... pass
...
>>> foo.a = 1 # Works with function objects too.
>>> foo.a
1
>>>
So, when you do:
xBox.textvariable = x
...
yBox.textvariable = y
you are not actually setting the Entrys' textvariable options to x and y. Instead, you are creating new attributes named textvariable on each of those objects.
To fix the problem, either set each Entry's textvariable option when you create the widgets:
xBox = tk.Entry(win1, textvariable=x)
...
yBox = tk.Entry(win1, textvariable=y)
or use the .config method to change them later:
xBox.config(textvariable=x)
...
yBox.config(textvariable=y)

Combined Event Handling of widgets in TKinter

I am making a GUI Program in Tkinter and am running into problems.What I want to do is draw 2 checkboxes and a button. According to the user input next steps should take place. A part of my code has been shown below :-
CheckVar1 = IntVar()
CheckVar2 = IntVar()
self.C1 = Checkbutton(root, text = "C Classifier", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5,width = 20).grid(row=4)
self.C2 = Checkbutton(root, text = "GClassifier", variable = CheckVar2, onvalue = 1, offvalue = 0, height=5, width = 20).grid(row=5)
self.proceed1 = Button(root,text = "\n Proceed",command = self.proceed(CheckVar1.get(),CheckVar2.get())).grid(row=6)
# where proceed prints the combined values of 2 checkboxes
The error that I am getting is typical ie a default value of both the selected checkboxes gets printed up and then there is no further input. The error that I get is NullType Object is not callable.
I searched on the net and I think the answer is related to lambda events or curry.
Please help ..
You're passing the value of self.proceed(CheckVar1.get(),CheckVar2.get()) to the Button constructor, but presumably what you want is for command to be set to a function which will call self.proceed(CheckVar1.get(),CheckVar2.get()) and return a new, possibly different value every time the button is pressed. You can fix that with a lambda, or by wrapping the call in a short callback function. For example, replace the last line with:
def callback():
return self.proceed(CheckVar1.get(), CheckVar2.get())
self.proceed1 = Button(root, text="\n Proceed", command=callback).grid(row=6)
This is pretty typical Tkinter. Remember: when you see a variable called command in Tkinter, it's looking for a function, not a value.
EDIT: to be clear: you're getting 'NullType Object is not callable' because you've set command to equal the return value of a single call to self.proceed (that's the NullType Object). self.proceed is a function, but its return value is not. What you need is to set command to be a function which calls self.proceed.
Like Peter Milley said, the command option needs a reference to a function (ie: give it a function name (ie: no parenthesis). Don't try to "inline" something, create a special function. Your code will be easier to understand and to maintain.

Categories

Resources