Having trouble with adding integers with Python GUI
up vote
0
down vote
favorite
I've been learning Python for the past 4 months now and I'm currentl learning about GUI applications. I'm having trouble trying to add integers after their selected with a CheckBox. The integers do add up but I'm getting these crazy results.
Here's my code:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
self.message += str(chargeTotal)
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
self.message += str(chargeTotal)
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
self.message += str(chargeTotal)
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
self.message += str(chargeTotal)
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
self.message += str(chargeTotal)
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
And this is the crazy result I keep getting:
https://imgur.com/a/qwIpTrn
I know it has to be a simple solution but I'm still very new to Python and can't seem to figure it out
python user-interface
add a comment |
up vote
0
down vote
favorite
I've been learning Python for the past 4 months now and I'm currentl learning about GUI applications. I'm having trouble trying to add integers after their selected with a CheckBox. The integers do add up but I'm getting these crazy results.
Here's my code:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
self.message += str(chargeTotal)
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
self.message += str(chargeTotal)
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
self.message += str(chargeTotal)
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
self.message += str(chargeTotal)
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
self.message += str(chargeTotal)
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
And this is the crazy result I keep getting:
https://imgur.com/a/qwIpTrn
I know it has to be a simple solution but I'm still very new to Python and can't seem to figure it out
python user-interface
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've been learning Python for the past 4 months now and I'm currentl learning about GUI applications. I'm having trouble trying to add integers after their selected with a CheckBox. The integers do add up but I'm getting these crazy results.
Here's my code:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
self.message += str(chargeTotal)
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
self.message += str(chargeTotal)
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
self.message += str(chargeTotal)
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
self.message += str(chargeTotal)
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
self.message += str(chargeTotal)
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
And this is the crazy result I keep getting:
https://imgur.com/a/qwIpTrn
I know it has to be a simple solution but I'm still very new to Python and can't seem to figure it out
python user-interface
I've been learning Python for the past 4 months now and I'm currentl learning about GUI applications. I'm having trouble trying to add integers after their selected with a CheckBox. The integers do add up but I'm getting these crazy results.
Here's my code:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
self.message += str(chargeTotal)
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
self.message += str(chargeTotal)
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
self.message += str(chargeTotal)
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
self.message += str(chargeTotal)
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
self.message += str(chargeTotal)
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
And this is the crazy result I keep getting:
https://imgur.com/a/qwIpTrn
I know it has to be a simple solution but I'm still very new to Python and can't seem to figure it out
python user-interface
python user-interface
asked Nov 22 at 1:27
Keyaki
63
63
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
You need to remove the self.message += str(chargeTotal) in each if statement and then put it as in code below. Hope it helps.
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
add a comment |
up vote
0
down vote
what you're doing is concatenating numbers converted to strings, at each if statement to your self.message.
eg. if you enter the first two if statements, you first get "Your total charge = $30", and after that, "Your total charge = $3020"
Rather, you should add the chargeTotal to your self.message at the end, only once:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422680%2fhaving-trouble-with-adding-integers-with-python-gui%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You need to remove the self.message += str(chargeTotal) in each if statement and then put it as in code below. Hope it helps.
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
add a comment |
up vote
0
down vote
You need to remove the self.message += str(chargeTotal) in each if statement and then put it as in code below. Hope it helps.
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to remove the self.message += str(chargeTotal) in each if statement and then put it as in code below. Hope it helps.
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
You need to remove the self.message += str(chargeTotal) in each if statement and then put it as in code below. Hope it helps.
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
answered Nov 22 at 1:33
Josh21
638
638
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
add a comment |
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
Wow. I knew I was close. Thanks.
– Keyaki
Nov 22 at 6:52
add a comment |
up vote
0
down vote
what you're doing is concatenating numbers converted to strings, at each if statement to your self.message.
eg. if you enter the first two if statements, you first get "Your total charge = $30", and after that, "Your total charge = $3020"
Rather, you should add the chargeTotal to your self.message at the end, only once:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
add a comment |
up vote
0
down vote
what you're doing is concatenating numbers converted to strings, at each if statement to your self.message.
eg. if you enter the first two if statements, you first get "Your total charge = $30", and after that, "Your total charge = $3020"
Rather, you should add the chargeTotal to your self.message at the end, only once:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
add a comment |
up vote
0
down vote
up vote
0
down vote
what you're doing is concatenating numbers converted to strings, at each if statement to your self.message.
eg. if you enter the first two if statements, you first get "Your total charge = $30", and after that, "Your total charge = $3020"
Rather, you should add the chargeTotal to your self.message at the end, only once:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
what you're doing is concatenating numbers converted to strings, at each if statement to your self.message.
eg. if you enter the first two if statements, you first get "Your total charge = $30", and after that, "Your total charge = $3020"
Rather, you should add the chargeTotal to your self.message at the end, only once:
def Calculate(self):
self.message = "Your total charge = $"
chargeTotal = 0
if self.checkBoxVar1.get() == 1:
chargeTotal += 30
if self.checkBoxVar2.get() == 1:
chargeTotal += 20
if self.checkBoxVar3.get() == 1:
chargeTotal += 40
if self.checkBoxVar4.get() == 1:
chargeTotal += 100
if self.checkBoxVar5.get() == 1:
chargeTotal += 35
if self.checkBoxVar6.get() == 1:
chargeTotal += 200
if self.checkBoxVar7.get() == 1:
chargeTotal += 20
self.message += str(chargeTotal)
tkinter.messagebox.showinfo("Total Charges", self.message)
answered Nov 22 at 1:39
pooh17
444
444
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
add a comment |
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
Yea, I figured I was doing something like that, now that I look back at the code. Thanks. Imposter Syndrome sucks.
– Keyaki
Nov 22 at 6:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422680%2fhaving-trouble-with-adding-integers-with-python-gui%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown