#๐Ÿ”’ tkinter calculator. multiply dont works

36 messages ยท Page 1 of 1 (latest)

cinder blazeBOT
#

@void vapor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

void vapor
#

import customtkinter as tk

tk.set_appearance_mode("dark")
tk.set_default_color_theme("green")

root = tk.CTk()
root.geometry("500x350")
txt = ""
skl1 = 0
skl2 = 0
wyn = 0
m = 0
a = 0
d = 0

def add(value):
global txt

txt += str(value)
lbl.configure(text=txt)
global a,d,m
a = 1
d = 0
m = 0

def addbutton():
global txt, skl1
skl1 = txt
txt = ""
lbl.configure(text=txt)

def equal():
global txt, skl1, skl2, wyn, a, d, m
if a == 1:
skl2 = txt
wyn = int(skl1) + int(skl2)
txt = str(wyn) # Poprawione przypisanie wyniku do txt
lbl.configure(text=txt)
elif m == 1:
skl2 = txt
wyn = int(skl1) * int(skl2)
txt = str(wyn) # Poprawione przypisanie wyniku do txt
lbl.configure(text=txt)
elif d == 1:
skl2 = txt
wyn = int(skl1) / int(skl2)
txt = str(wyn) # Poprawione przypisanie wyniku do txt
lbl.configure(text=txt)
part 1

#

def clear():
global txt,wyn,skl1,skl2
lbl.configure(text="")
skl1 = 0
skl2 = 0
txt = ""
wyn = 0

def multiply():
global txt, skl1, a, d, m
skl1 = txt
txt = ""
lbl.configure(text=txt)
a = 0
d = 0
m = 1
lbl.configure(text=skl1)

lbl = tk.CTkLabel(master=root, text="", font=("Roboto", 20))
lbl.grid(row=0, column=0, padx=0, pady=50)

btn1 = tk.CTkButton(master=root, text="1", command=lambda: add(1))
btn1.grid(row=1, column=0, padx=5, pady=5)
btn2 = tk.CTkButton(master=root, text="2", command=lambda: add(2))
btn2.grid(row=1, column=1, padx=5, pady=5)
btn3 = tk.CTkButton(master=root, text="3", command=lambda: add(3))
btn3.grid(row=1, column=2, padx=5, pady=5)
btn4 = tk.CTkButton(master=root, text="4", command=lambda: add(4))
btn4.grid(row=2, column=0, padx=5, pady=5)
btn5 = tk.CTkButton(master=root, text="5", command=lambda: add(5))
btn5.grid(row=2, column=1, padx=5, pady=5)
btn6 = tk.CTkButton(master=root, text="6", command=lambda: add(6))
btn6.grid(row=2, column=2, padx=5, pady=5)
btn7 = tk.CTkButton(master=root, text="7", command=lambda: add(7))
btn7.grid(row=3, column=0, padx=5, pady=5)
btn8 = tk.CTkButton(master=root, text="8", command=lambda: add(8))
btn8.grid(row=3, column=1, padx=5, pady=5)
btn9 = tk.CTkButton(master=root, text="9", command=lambda: add(9))
btn9.grid(row=3, column=2, padx=5, pady=5)
btn0 = tk.CTkButton(master=root, text="0", command=lambda: add(0))
btn0.grid(row=4, column=1, padx=5, pady=5)

btnadd = tk.CTkButton(master=root, text="+", command=addbutton)
btnadd.grid(row=4, column=0, padx=5, pady=5)

btnadd = tk.CTkButton(master=root, text="=", command=equal)
btnadd.grid(row=4, column=2, padx=5, pady=5)

btnclear = tk.CTkButton(master=root, text="C", command=clear)
btnclear.grid(row=5, column=0, padx=5, pady=5)

btnmultiply = tk.CTkButton(master=root, text="*", command=multiply)
btnmultiply.grid(row=5, column=1, padx=5, pady=5)

root.mainloop()

part 2

#

that calculator says me "12 * 2 = 14 "

deft hill
#

!code

cinder blazeBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

void vapor
#

message is too long

deft hill
#

!paste

cinder blazeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

deft hill
#
def multiply():
    global txt, skl1, a, d, m
    skl1 = txt
    txt = ""
    lbl.configure(text=txt)
    a = 0
    d = 0
    m = 1
    lbl.configure(text=skl1)

I don't see any math being done here though.

void vapor
#

look at the equal

deft hill
#

Is this function meant to multiply numbers?

void vapor
#

which one

deft hill
#

Oh, I see, m = 1 is indicating they pressed multiply.

void vapor
#

yes and if i press equalbutton then its checking
whether the variable m is 1 and then performs the action

deft hill
#

Well, the output suggests that a is also 1.

#

And a == 1 is checked first.

void vapor
#

yes

#

but on the start of the script it writen a = 0

deft hill
#

add sets it to 1 though. I'm assuming the user is pressing the number buttons before hitting multiply?

void vapor
#

but why multiply is not working when a == 0 and m == 1

deft hill
#

Because a == 1.

void vapor
#

oh i see that

deft hill
#

Did you mean for command=lambda: add(0) to be command=lambda: addbutton() or something?

drifting knoll
#

btnadd = tk.CTkButton(master=root, text="+", command=addbutton)
btnadd.grid(row=4, column=0, padx=5, pady=5)

btnadd = tk.CTkButton(master=root, text="=", command=equal)
btnadd.grid(row=4, column=2, padx=5, pady=5)
why do you have two btnadd when they are supposed to be different usages

void vapor
#

in the wrong function a = 1 is writen

deft hill
# void vapor in the wrong function a = 1 is writen

Your state management here is a bit of a mess. I would get rid of m, a, and d, and replace them with a single variable called something like operation. Then either make operation an Enum, or use some other set of values to indicate the operation.

#

Presumably, it doesn't make sense for both add and multiply to be selected at once?

void vapor
#

i forgot add and addbutton is 2 other function

#

oh its working now

#

thank you

deft hill
#

Assuming it was my help: You're welcome.

void vapor
#

yes i know that tysm

cinder blazeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.