admin管理员组文章数量:1336568
I have a pair of boxes. One is for labor hours. The other is for labor dollars. When you enter a labor hour amount, it automatically calculates and fills the labor dollar box. When you enter a labor dollar amount, it automatically calculates and fills the labor hours box. Everything works as desired. I use a variable.get() in the function to retrieve the data from the Entry boxes as the numbers are typed, and then update the math on a keystroke by keystroke basis.
The problem I'm running into is that when the labor dollars equate to a long decimal, the calculations go slightly awry. What ends up happening is that at a labor rate of 70.00 per hour, if I input a labor dollar amount of, for example, 127.00, it automatically fills/displays 1.81 hours of labor, which is exactly what I want to happen. But 1.81 hours of labor at 70.00 per hour comes to 126.70 because 127.00 divided by 70.00 per hour is 1.814285714285714 hours of labor, not 1.81.
As a result, the binding, which runs by simply letting go of the tab button, causes the input labor dollars to change from 127.00 to 126.70. To get the numbers to stay the same, I need it to be able to calculate according to the full decimal number of 1.814285714285714 * 70.00, but for aesthetic purposes, I only want it to display the first two decimal places.
I don't know where to start looking to be able to calculate with the full number while displaying a truncated version.
I can offer the code I'm using as needed, but I think the question is pretty straight forward. If anyone can offer constructive guidance, it would be appreciated.
Edit: Code added
IE = {}
for i in range(117):
IE[i] = None
def lh_1(event):
lha = lhbox1.get()
if lha.replace('.','',1).isnumeric() == True:
IE[7] = float(lhbox1.get())
IE[8] = float(IE[7])*float(ratesbox1.get())
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
print(IE[7])
print(IE[8])
elif lhbox1.get() == ".":
IE[7] = lhbox1.get()
lbox1.delete(0, tkinter.END)
elif lhbox1.get() == "":
lhbox1.delete(0, tkinter.END)
IE[7] = None
lbox1.delete(0, tkinter.END)
else:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, IE[7])
def lh_1_enter(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
def lh_1_leave(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
lhfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=48, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lhfrm1.place(x=902, y=110)
lhbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=6, borderwidth=0, relief=tkinter.FLAT)
lhbox1.place(x=908, y=114)
WOBOXES.append(lhbox1)
lhbox1.bind("<KeyRelease>", lh_1)
lhbox1.bind("<FocusIn>", lh_1_enter)
lhbox1.bind("<FocusOut>", lh_1_leave)
def l_1(event):
la = lbox1.get()
if la.replace('.','',1).isnumeric() == True:
IE[8] = float(lbox1.get())
IE[7] = float(IE[8])/float(ratesbox1.get())
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(IE[7]))
print(IE[8])
print(IE[7])
elif lbox1.get() == ".":
IE[8] = lbox1.get()
lhbox1.delete(0, tkinter.END)
elif lbox1.get() == "":
lbox1.delete(0, tkinter.END)
IE[8] = None
lhbox1.delete(0, tkinter.END)
else:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_enter(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_leave(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
lfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=70, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lfrm1.place(x=952, y=110)
lbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=10, borderwidth=0, relief=tkinter.FLAT)
lbox1.place(x=958, y=114)
WOBOXES.append(lbox1)
lbox1.bind("<KeyRelease>", l_1)
lbox1.bind("<FocusIn>", l_1_enter)
lbox1.bind("<FocusOut>", l_1_leave)
I have a pair of boxes. One is for labor hours. The other is for labor dollars. When you enter a labor hour amount, it automatically calculates and fills the labor dollar box. When you enter a labor dollar amount, it automatically calculates and fills the labor hours box. Everything works as desired. I use a variable.get() in the function to retrieve the data from the Entry boxes as the numbers are typed, and then update the math on a keystroke by keystroke basis.
The problem I'm running into is that when the labor dollars equate to a long decimal, the calculations go slightly awry. What ends up happening is that at a labor rate of 70.00 per hour, if I input a labor dollar amount of, for example, 127.00, it automatically fills/displays 1.81 hours of labor, which is exactly what I want to happen. But 1.81 hours of labor at 70.00 per hour comes to 126.70 because 127.00 divided by 70.00 per hour is 1.814285714285714 hours of labor, not 1.81.
As a result, the binding, which runs by simply letting go of the tab button, causes the input labor dollars to change from 127.00 to 126.70. To get the numbers to stay the same, I need it to be able to calculate according to the full decimal number of 1.814285714285714 * 70.00, but for aesthetic purposes, I only want it to display the first two decimal places.
I don't know where to start looking to be able to calculate with the full number while displaying a truncated version.
I can offer the code I'm using as needed, but I think the question is pretty straight forward. If anyone can offer constructive guidance, it would be appreciated.
Edit: Code added
IE = {}
for i in range(117):
IE[i] = None
def lh_1(event):
lha = lhbox1.get()
if lha.replace('.','',1).isnumeric() == True:
IE[7] = float(lhbox1.get())
IE[8] = float(IE[7])*float(ratesbox1.get())
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
print(IE[7])
print(IE[8])
elif lhbox1.get() == ".":
IE[7] = lhbox1.get()
lbox1.delete(0, tkinter.END)
elif lhbox1.get() == "":
lhbox1.delete(0, tkinter.END)
IE[7] = None
lbox1.delete(0, tkinter.END)
else:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, IE[7])
def lh_1_enter(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
def lh_1_leave(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
lhfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=48, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lhfrm1.place(x=902, y=110)
lhbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=6, borderwidth=0, relief=tkinter.FLAT)
lhbox1.place(x=908, y=114)
WOBOXES.append(lhbox1)
lhbox1.bind("<KeyRelease>", lh_1)
lhbox1.bind("<FocusIn>", lh_1_enter)
lhbox1.bind("<FocusOut>", lh_1_leave)
def l_1(event):
la = lbox1.get()
if la.replace('.','',1).isnumeric() == True:
IE[8] = float(lbox1.get())
IE[7] = float(IE[8])/float(ratesbox1.get())
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(IE[7]))
print(IE[8])
print(IE[7])
elif lbox1.get() == ".":
IE[8] = lbox1.get()
lhbox1.delete(0, tkinter.END)
elif lbox1.get() == "":
lbox1.delete(0, tkinter.END)
IE[8] = None
lhbox1.delete(0, tkinter.END)
else:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_enter(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_leave(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
lfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=70, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lfrm1.place(x=952, y=110)
lbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=10, borderwidth=0, relief=tkinter.FLAT)
lbox1.place(x=958, y=114)
WOBOXES.append(lbox1)
lbox1.bind("<KeyRelease>", l_1)
lbox1.bind("<FocusIn>", l_1_enter)
lbox1.bind("<FocusOut>", l_1_leave)
Share
Improve this question
edited Nov 20, 2024 at 4:21
AFrazier
asked Nov 20, 2024 at 4:00
AFrazierAFrazier
1471 silver badge6 bronze badges
18
- 1 You can print a number the way you like. If I print a black and white photo of myself I don't turn black and white myself... How to Ask and minimal reproducible example – Julien Commented Nov 20, 2024 at 4:03
- The answer is going to depend on the GUI toolkit you're using. A general solution might be to add a "cached" number which is full precision, and only update the cache if the user types into the corresponding box. – nneonneo Commented Nov 20, 2024 at 4:04
- 1 It's ok and commendable to be learning. Just SO is not a replacement for reading basic tutorials. Also have you tried simply searching "python display float with 2 decimals" – Julien Commented Nov 20, 2024 at 4:12
- 1 @Julien I think the difficulty is not the display itself, but how to ensure that an editable textbox doesn't immediately overwrite the high-precision value with the low-precision value that's displayed when the user simply tabs past the field. I agree the title is not written very clearly. – nneonneo Commented Nov 20, 2024 at 4:13
- 1 Okay, I've added the relevant code. – AFrazier Commented Nov 20, 2024 at 4:19
1 Answer
Reset to default 1So, here's how I solved the problem.
First, I changed the event to a <KeyPress>
rather than a <KeyRelease>
. By doing this, it didn't automatically run the event just by tabbing into the Entry box. Releasing the tab key was triggering the <KeyRelease>
event after it was already in the box, which would then cause it to variable.get()
the contents of the Entry box and make the calculations that were causing my problem. But with a <KeyPress>
, the event isn't triggered, because the key press happens before entering the Entry box.
Next, I added the keyboard
module with a pip3 install keyboard
from the cmd prompt.
Then I updated the code for the def lh_1(event)
to include a simple if
statement.
if keyboard.read_key() != "tab":
Once I am in the Entry box, if any key is pressed except the tab key, it runs the rest of the function, testing that the number is numeric, whether it contains a decimal, etc. But if it is the tab key, then it does nothing and the stored values in the dictionary remain the same. This essentially checks whether I'm actually changing the contents of the Entry box or just tabbing through it. The final code looks like this:
def lh_1(event):
if keyboard.read_key() != "tab":
lha = lhbox1.get()
if lha.replace('.','',1).isnumeric() == True:
IE[7] = float(lhbox1.get())
IE[8] = float(IE[7])*float(ratesbox1.get())
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
elif lhbox1.get() == ".":
IE[7] = lhbox1.get()
lbox1.delete(0, tkinter.END)
elif lhbox1.get() == "":
lhbox1.delete(0, tkinter.END)
IE[7] = None
lbox1.delete(0, tkinter.END)
else:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, IE[7])
def lh_1_enter(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
def lh_1_leave(event):
if IE[7] != None:
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(float(IE[7])))
lhfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=48, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lhfrm1.place(x=902, y=110)
lhbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=6, borderwidth=0, relief=tkinter.FLAT)
lhbox1.place(x=908, y=114)
WOBOXES.append(lhbox1)
lhbox1.bind("<KeyPress>", lh_1)
lhbox1.bind("<FocusIn>", lh_1_enter)
lhbox1.bind("<FocusOut>", lh_1_leave)
def l_1(event):
la = lbox1.get()
if la.replace('.','',1).isnumeric() == True:
IE[8] = float(lbox1.get())
IE[7] = float(IE[8])/float(ratesbox1.get())
lhbox1.delete(0, tkinter.END)
lhbox1.insert(0, "{:.2f}".format(IE[7]))
elif lbox1.get() == ".":
IE[8] = lbox1.get()
lhbox1.delete(0, tkinter.END)
elif lbox1.get() == "":
lbox1.delete(0, tkinter.END)
IE[8] = None
lhbox1.delete(0, tkinter.END)
else:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_enter(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "{:.2f}".format(float(IE[8])))
def l_1_leave(event):
if IE[8] != None:
lbox1.delete(0, tkinter.END)
lbox1.insert(0, "${:.2f}".format(float(IE[8])))
lfrm1 = tkinter.Frame(master=wofrm, bg="#b2b4b7", width=70, height=25, borderwidth=2, relief=tkinter.SUNKEN)
lfrm1.place(x=952, y=110)
lbox1 = tkinter.Entry(master=wofrm, bg="#b2b4b7", fg="#000000", width=10, borderwidth=0, relief=tkinter.FLAT)
lbox1.place(x=958, y=114)
WOBOXES.append(lbox1)
lbox1.bind("<KeyPress>", l_1)
lbox1.bind("<FocusIn>", l_1_enter)
lbox1.bind("<FocusOut>", l_1_leave)
本文标签:
版权声明:本文标题:tkinter - In Python, can you get the full utility of a long decimal while only displaying two decimal places? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742383245a2464512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论