pycharm编译乘法计算问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pycharm编译乘法计算问题?相关的知识,希望对你有一定的参考价值。
pycharm编译中,输出乘法计算结果,有时小数点后有十六位数,这是为什么?
在计算机中,浮点数是以二进制存储的,而不是十进制。在十进制下,有些小数可以精确表示,例如 0.5 或 0.25 等。但是,在二进制下,某些分数可能无法准确表示,因为分数的分母可能无法被二进制表示。例如,0.1 无法准确表示为二进制小数,因为 0.1 在二进制下是无限循环的。这就导致了在进行浮点数计算时可能出现舍入误差,即计算结果可能有一些小数位不准确。Python 中的浮点数类型也是如此。
对于你提到的情况,输出结果小数点后有16位数字,这可能是由于浮点数计算过程中出现了舍入误差导致的。你可以尝试使用 Decimal 类型来处理浮点数,该类型可以提供更高的精度,以避免舍入误差。例如,可以使用以下代码来将浮点数 a 和 b 相乘并保留 10 位小数:
scssCopy codefrom decimal import Decimala = 3.14159b = 2.71828result = Decimal(str(a)) * Decimal(str(b))print(round(result, 10))
在这个例子中,我们将浮点数 a 和 b 先转换为字符串,再使用 Decimal 类型进行计算,并使用 round() 函数将结果保留 10 位小数。 参考技术A 这是由于浮点数在计算机中的存储和表示方式导致的。
计算机内部采用二进制表示浮点数,但是很多十进制小数无法精确地用有限的二进制位表示出来,因此在计算机内部进行计算时,可能会出现精度损失的情况。
在Python中,使用float类型来表示浮点数,float类型在内存中占用8个字节(64位),其中一部分用来表示小数点的位置,一部分用来表示小数的有效数字。
由于计算机在二进制下表示小数时有精度限制,因此在进行乘法计算时,可能会出现小数点后面有十六位数的情况。
为了避免这种情况,可以使用Decimal类型来表示浮点数,它可以精确表示任意精度的小数,但是会导致计算效率降低。在实际开发中,需要根据具体情况选择合适的数据类型来进行计算。
如何在pycharm python上从3到6的按钮进行乘法和求和
我正在学习使用python和tkinter制作应用程序。然后我做了一个电阻计算器。问题是如何计算几个按钮的相乘和相加以获得值并将总结果写入电阻值输入框中?
更糟糕的是,我不知道如何在此处编写代码。所以我给了链接。
非常感谢
import tkinter.messagebox
from tkinter import*
from tkinter import ttk
class Resistor:
def __init__(self, root):
self.root = root
self.root.title("Roghib Calculator Resistor")
self.root.geometry("1000x560+0+0")
self.root.configure(background="aqua")
var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var1.set("")
var2.set("")
var3.set("")
var4.set("")
var5.set("")
var6.set("")
var7.set("")
var8.set("")
var9.set("")
#value 1stband
def Band1b():
var1.set(10)
def Band1c():
var1.set(20)
def Band1d():
var1.set(30)
def Band1e():
var1.set(40)
def Band1f():
var1.set(50)
def Band1g():
var1.set(60)
def Band1h():
var1.set(70)
def Band1i():
var1.set(80)
def Band1j():
var1.set(90)
#value 2ndband
def Band2a():
var2.set(0)
def Band2b():
var2.set(1)
def Band2c():
var2.set(2)
def Band2d():
var2.set(3)
def Band2e():
var2.set(4)
def Band2f():
var2.set(5)
def Band2g():
var2.set(6)
def Band2h():
var2.set(7)
def Band2i():
var2.set(8)
def Band2j():
var2.set(9)
def hitung():
var1 = var1.get()
var2 = var2.get()
def quit():
quit = tkinter.messagebox.askyesno('Calculator Resistor', 'yakin arep metu?')
if quit > 0:
root.destroy()
return
def reset():
reset = tkinter.messagebox.askyesno('Calculator Resistor', 'yakin arep reset ?')
if reset > 0:
var1.set("")
return
#frame = Frame(root)
#frame.grid()
mainFrame = Frame(self.root, bg='olive')
mainFrame.grid()
TitleFrame = Frame(mainFrame, bg='yellow', bd=10, width=1320, padx=10, relief=RIDGE)
TitleFrame.grid(row=0, column=0, columnspan=2)
self.lblTitle = Label(TitleFrame, bg='yellow', font=('times new roman', 50, 'bold'), text="Calculator Resistor", padx=200)
self.lblTitle.grid(row=0, column=0)
ResistorFrame = Frame(mainFrame, bd=10, width=1350, padx=20, bg='dimgrey', relief=RIDGE)
ResistorFrame.grid(row=1, column=0, sticky=W)
IndicatorFrame = Frame(mainFrame, bg='yellow', bd=10, width=1350, padx=10, relief=RIDGE)
IndicatorFrame.grid(row=1, column=1, sticky=W)
#===============================================label Band Title===================================================
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'),text="1st Band")
self.lblTitle.grid(row=1, column=0)
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'), text="2nd Band")
self.lblTitle.grid(row=1, column=1)
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'), text="3rd Band")
self.lblTitle.grid(row=1, column=2)
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'), text="4th Band")
self.lblTitle.grid(row=1, column=3)
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'), text="5th Band")
self.lblTitle.grid(row=1, column=4)
self.lblTitle = Label(ResistorFrame, bg="orangered", width=8, font=('times new roman', 14, 'bold'), text="6th Band")
self.lblTitle.grid(row=1, column=5)
#option total Band
self.dis = Radiobutton(ResistorFrame, text="3 Band", font='bold', value=0, bg='dimgrey', command=self.enable_band3)
self.dis.grid(column=1, row=0)
self.en = Radiobutton(ResistorFrame, text="6 Band", font='bold', value=1, bg='dimgrey', command=self.disable_band3)
self.en.grid(column=4, row=0)
self.dis4 = Radiobutton(ResistorFrame, text="4 Band", font='bold', value=2, bg='dimgrey', command=self.enable_band4)
self.dis4.grid(column=2, row=0)
self.en4 = Radiobutton(ResistorFrame, text="5 Band", font='bold', value=3, bg='dimgrey', command=self.disable_band4)
self.en4.grid(column=3, row=0)
#1st Band
self.Band1a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='0', fg='white', bg='black')
self.Band1a.grid(row=2, column=0)
self.Band1b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1b, text='1', fg='black', bg='peru')
self.Band1b.grid(row=3, column=0)
self.Band1c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1c, text='2', fg='black', bg='red')
self.Band1c.grid(row=4, column=0)
self.Band1d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1d, text='3', fg='black', bg='orange')
self.Band1d.grid(row=5, column=0)
self.Band1e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1e, text='4', fg='black', bg='yellow')
self.Band1e.grid(row=6, column=0)
self.Band1f = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1f, text='5', fg='black', bg='green')
self.Band1f.grid(row=7, column=0)
self.Band1g = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1g, text='6', fg='black', bg='blue')
self.Band1g.grid(row=8, column=0)
self.Band1h = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1h, text='7', fg='black', bg='purple')
self.Band1h.grid(row=9, column=0)
self.Band1i = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1i, text='8', fg='black', bg='dimgrey')
self.Band1i.grid(row=10, column=0)
self.Band1j = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band1j, text='9', fg='black', bg='white')
self.Band1j.grid(row=11, column=0)
#2nd Band
self.Band2a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band2a, text='0', fg='white', bg='black')
self.Band2a.grid(row=2, column=1)
self.Band2b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), command=Band2b, text='1', fg='black', bg='peru')
self.Band2b.grid(row=3, column=1)
self.Band2c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='2', fg='black', bg='red')
self.Band2c.grid(row=4, column=1)
self.Band2d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='3', fg='black', bg='orange')
self.Band2d.grid(row=5, column=1)
self.Band2e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='4', fg='black', bg='yellow')
self.Band2e.grid(row=6, column=1)
self.Band2f = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='5', fg='black', bg='green')
self.Band2f.grid(row=7, column=1)
self.Band2g = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='6', fg='black', bg='blue')
self.Band2g.grid(row=8, column=1)
self.Band2h = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='7', fg='black', bg='purple')
self.Band2h.grid(row=9, column=1)
self.Band2i = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='8', fg='black', bg='dimgrey')
self.Band2i.grid(row=10, column=1)
self.Band2j = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='9', fg='black', bg='white')
self.Band2j.grid(row=11, column=1)
#3rd Band
self.Band3a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='0', fg='white', bg='black')
self.Band3a.grid(row=2, column=2)
self.Band3b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='1', fg='black', bg='peru')
self.Band3b.grid(row=3, column=2)
self.Band3c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='2', fg='black', bg='red')
self.Band3c.grid(row=4, column=2)
self.Band3d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='3', fg='black', bg='orange')
self.Band3d.grid(row=5, column=2)
self.Band3e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='4', fg='black', bg='yellow')
self.Band3e.grid(row=6, column=2)
self.Band3f = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='5', fg='black', bg='green')
self.Band3f.grid(row=7, column=2)
self.Band3g = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='6', fg='black', bg='blue')
self.Band3g.grid(row=8, column=2)
self.Band3h = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='7', fg='black', bg='purple')
self.Band3h.grid(row=9, column=2)
self.Band3i = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='8', fg='black', bg='dimgrey')
self.Band3i.grid(row=10, column=2)
self.Band3j = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='9', fg='black', bg='white')
self.Band3j.grid(row=11, column=2)
#4th Band
self.Band4a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x1', fg='white', bg='black')
self.Band4a.grid(row=2, column=3)
self.Band4b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x10', fg='black', bg='peru')
self.Band4b.grid(row=3, column=3)
self.Band4c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x100', fg='black', bg='red')
self.Band4c.grid(row=4, column=3)
self.Band4d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x1k', fg='black', bg='orange')
self.Band4d.grid(row=5, column=3)
self.Band4e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x10k', fg='black', bg='yellow')
self.Band4e.grid(row=6, column=3)
self.Band4f = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x100k', fg='black', bg='green')
self.Band4f.grid(row=7, column=3)
self.Band4g = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x1M', fg='black', bg='blue')
self.Band4g.grid(row=8, column=3)
self.Band4h = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x10M', fg='black', bg='purple')
self.Band4h.grid(row=9, column=3)
self.Band4a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x0.01', fg='black', bg='darkgray')
self.Band4a.grid(row=10, column=3)
self.Band4b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='x0.1', fg='black', bg='darkgoldenrod')
self.Band4b.grid(row=11, column=3)
#5thband
self.Band5a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='1%', fg='black', bg='peru')
self.Band5a.grid(row=3, column=4)
self.Band5b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='2%', fg='black', bg='red')
self.Band5b.grid(row=4, column=4)
self.Band5c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='0.5%', fg='black', bg='green')
self.Band5c.grid(row=7, column=4)
self.Band5d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='0.25%', fg='black', bg='blue')
self.Band5d.grid(row=8, column=4)
self.Band5e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='0.1%', fg='black', bg='purple')
self.Band5e.grid(row=9, column=4)
self.Band5g = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='10%', fg='black', bg='darkgray')
self.Band5g.grid(row=10, column=4)
self.Band5f = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='5%', fg='black', bg='darkgoldenrod')
self.Band5f.grid(row=11, column=4)
#6th Band
self.Band6a = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='250ppm/K', fg='white', bg='black')
self.Band6a.grid(row=2, column=5)
self.Band6b = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='100ppm/K', fg='black', bg='peru')
self.Band6b.grid(row=3, column=5)
self.Band6c = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='50ppm/K', fg='black', bg='red')
self.Band6c.grid(row=4, column=5)
self.Band6d = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='15ppm/K', fg='black', bg='orange')
self.Band6d.grid(row=5, column=5)
self.Band6e = Button(ResistorFrame, width=8, font=('times new roman', 14, 'bold'), text='25ppm/K', fg='black', bg='yellow')
self.Band6e.grid(row=6, column=5)
#Indicator Frame
self.lblFirst = Label(IndicatorFrame, bg='yellow', font=('times new roman', 15, 'bold'), text="Resistor Value")
self.lblFirst.grid(row=0, column=0, sticky=W, padx=10)
self.txtFirst = Entry(IndicatorFrame, width=16, font=('times new roman', 15, 'bold'), textvariable=var1)
self.txtFirst.grid(row=0, column=1, pady=3, columnspan=3)
self.lbltole = Label(IndicatorFrame, bg='yellow', font=('times new roman', 15, 'bold'), text="Tolerance")
self.lbltole.grid(row=1, column=0, sticky=W, padx=10)
self.txtSecond = Entry(IndicatorFrame, width=16, font=('times new roman', 15, 'bold'), textvariable=var8)
self.txtSecond.grid(row=1, column=1, pady=10, columnspan=3)
self.temp = Label(IndicatorFrame, bg='yellow', font=('times new roman', 15, 'bold'), text="Temp. Coefficient")
self.temp.grid(row=2, column=0, sticky=W, padx=10)
self.txtthird = Entry(IndicatorFrame, width=16, font=('times new roman', 15, 'bold'), textvariable=var8)
self.txtthird.grid(row=2, column=1, pady=10, columnspan=5)
btnQuit = Button(IndicatorFrame, height=2, text="QUIT", width=8, command=quit)
btnQuit.grid(column=1, row=3, pady=10)
btnReset = Button(IndicatorFrame, height=2, text="RESET", command=reset, width=8)
btnReset.grid(column=2, row=3, pady=10)
btnHitung = Button(IndicatorFrame, height=2, command=hitung, text="HITUNG", width=8)
btnHitung.grid(column=3, row=3, pady=10)
def enable_band3(self):
if self.en == 0:
self.Band3a.configure(state='normal')
self.Band3b.configure(state='normal')
self.Band3c.configure(state='normal')
self.Band3d.configure(state='normal')
self.Band3e.configure(state='normal')
self.Band3f.configure(state='normal')
self.Band3g.configure(state='normal')
self.Band3h.configure(state='normal')
self.Band3i.configure(state='normal')
self.Band3j.configure(state='normal')
self.Band5a.configure(state='normal')
self.Band5b.configure(state='normal')
self.Band5c.configure(state='normal')
self.Band5d.configure(state='normal')
self.Band5e.configure(state='normal')
self.Band5f.configure(state='normal')
self.Band5g.configure(state='normal')
self.Band6a.configure(state='normal')
self.Band6b.configure(state='normal')
self.Band6c.configure(state='normal')
self.Band6d.configure(state='normal')
else:
self.Band1a.configure(state='disabled')
self.Band3a.configure(state='disabled')
self.Band3b.configure(state='disabled')
self.Band3c.configure(state='disabled')
self.Band3d.configure(state='disabled')
self.Band3e.configure(state='disabled')
self.Band3f.configure(state='disabled')
self.Band3g.configure(state='disabled')
self.Band3h.configure(state='disabled')
self.Band3i.configure(state='disabled')
self.Band3j.configure(state='disabled')
self.Band5a.configure(state='disabled')
self.Band5b.configure(state='disabled')
self.Band5c.configure(state='disabled')
self.Band5d.configure(state='disabled')
self.Band5e.configure(state='disabled')
self.Band5f.configure(state='disabled')
self.Band5g.configure(state='disabled')
self.Band6a.configure(state='disabled')
self.Band6b.configure(state='disabled')
self.Band6c.configure(state='disabled')
self.Band6d.configure(state='disabled')
self.Band6e.configure(state='disabled')
def disable_band3(self):
if self.dis != 0:
self.Band1a.configure(state='normal')
self.Band3a.configure(state='normal')
self.Band3b.configure(state='normal')
self.Band3c.configure(state='normal')
self.Band3d.configure(state='normal')
self.Band3e.configure(state='normal')
self.Band3f.configure(state='normal')
self.Band3g.configure(state='normal')
self.Band3h.configure(state='normal')
self.Band3i.configure(state='normal')
self.Band3j.configure(state='normal')
self.Band5a.configure(state='normal')
self.Band5b.configure(state='normal')
self.Band5c.configure(state='normal')
self.Band5d.configure(state='normal')
self.Band5e.configure(state='normal')
self.Band5f.configure(state='normal')
self.Band5g.configure(state='normal')
self.Band6a.configure(state='normal')
self.Band6b.configure(state='normal')
self.Band6c.configure(state='normal')
self.Band6d.configure(state='normal')
self.Band6e.configure(state='normal')
def enable_band4(self):
if self.en4 == 1:
self.Band3a.configure(state='disabled')
self.Band3b.configure(state='disabled')
self.Band3c.configure(state='disabled')
self.Band3d.configure(state='disabled')
self.Band3e.configure(state='disabled')
self.Band3f.configure(state='disabled')
self.Band3g.configure(state='disabled')
self.Band3h.configure(state='disabled')
self.Band3i.configure(state='disabled')
self.Band3j.configure(state='disabled')
self.Band6a.configure(state='disabled')
self.Band6b.configure(state='disabled')
self.Band6c.configure(state='disabled')
self.Band6d.configure(state='disabled')
else:
self.Band3a.configure(state='disabled')
self.Band3b.configure(state='disabled')
self.Band3c.configure(state='disabled')
self.Band3d.configure(state='disabled')
self.Band3e.configure(state='disabled')
self.Band3f.configure(state='disabled')
self.Band3g.configure(state='disabled')
self.Band3h.configure(state='disabled')
self.Band3i.configure(state='disabled')
self.Band3j.configure(state='disabled')
self.Band5a.configure(state='normal')
self.Band5b.configure(state='normal')
self.Band5c.configure(state='normal')
self.Band5d.configure(state='normal')
self.Band5e.configure(state='normal')
self.Band5f.configure(state='normal')
self.Band5g.configure(state='normal')
self.Band6a.configure(state='disabled')
self.Band6b.configure(state='disabled')
self.Band6c.configure(state='disabled')
self.Band6d.configure(state='disabled')
def disable_band4(self):
if self.dis4 != 1:
self.Band1a.configure(state='normal')
self.Band3a.configure(state='normal')
self.Band3b.configure(state='normal')
self.Band3c.configure(state='normal')
self.Band3d.configure(state='normal')
self.Band3e.configure(state='normal')
self.Band3f.configure(state='normal')
self.Band3g.configure(state='normal')
self.Band3h.configure(state='normal')
self.Band3i.configure(state='normal')
self.Band3j.configure(state='normal')
self.Band5a.configure(state='normal')
self.Band5b.configure(state='normal')
self.Band5c.configure(state='normal')
self.Band5d.configure(state='normal')
self.Band5e.configure(state='normal')
self.Band5f.configure(state='normal')
self.Band5g.configure(state='normal')
self.Band6a.configure(state='normal')
self.Band6b.configure(state='normal')
self.Band6c.configure(state='normal')
self.Band6d.configure(state='normal')
self.Band6a.configure(state='disabled')
self.Band6b.configure(state='disabled')
self.Band6c.configure(state='disabled')
self.Band6d.configure(state='disabled')
self.Band6e.configure(state='disabled')
if __name__=='__main__':
root = Tk()
application = Resistor(root)
root.mainloop()
答案
在SetBand
之后的功能varX.set()
中,您可以执行计算值并在条目中更新它的功能。
其他更改(减少了代码行):
我将颜色和按钮文本保留在列表中,因此可以使用for
循环(带有zip()
和enumerate()
)来生成按钮。我将按钮保留在列表中,以便以后可以使用for
循环更改按钮的状态。
在command=
中,我使用lamba
,所以我可以为相同的函数分配不同的参数
完整的工作代码,但我不确定是否所有的工作都按预期进行。
import tkinter.messagebox
import tkinter as tk
from tkinter import ttk
class Resistor:
def set_band1(self, value):
self.var_band1.set(value)
self.update_labels()
def set_band2(self, value):
self.var_band2.set(value)
self.update_labels()
def set_band3(self, value):
self.var_band3.set(value)
self.update_labels()
def set_band4(self, value):
self.var_band4.set(value)
self.update_labels()
def set_band5(self, value):
self.var_band5.set(value)
self.update_labels()
def set_band6(self, value):
self.var_band6.set(value)
self.update_labels()
def update_labels(self):
v1 = self.var_band1.get()*100
v2 = self.var_band2.get()*10
v3 = self.var_band3.get()*1
value = (v1+v2+v3) * self.var_band4.get()
self.var_resistance.set(value)
value = self.var_band5.get()
self.var_tolerance.set(value)
value = self.var_band6.get()
self.var_temp.set(value)
def hitung(self):
var1 = self.var_band1.get()
var2 = self.var_band2.get()
def quit(self):
result = tkinter.messagebox.askyesno('Calculator Resistor', 'yakin arep metu?')
if result:
root.destroy()
def reset(self):
result = tkinter.messagebox.askyesno('Calculator Resistor', 'yakin arep reset ?')
if result:
self.var_band1.set(0)
def __init__(self, root):
self.root = root
self.root.title("Roghib Calculator Resistor")
#self.root.geometry("1000x560+0+0")
self.root.configure(background="aqua")
self.var_band1 = tk.IntVar(value=0)
self.var_band2 = tk.IntVar(value=0)
self.var_band3 = tk.IntVar(value=0)
self.var_band4 = tk.IntVar(value=1)
self.var_band5 = tk.StringVar(value='')
self.var_band6 = tk.StringVar(value='')
self.var_resistance = tk.StringVar(value='0')
self.var_tolerance = tk.StringVar(value='0')
self.var_temp = tk.StringVar(value='0')
# === Frames ===
main_frame = tk.Frame(self.root, bg='olive')
main_frame.grid()
title_frame = tk.Frame(main_frame, bg='yellow', bd=10, width=1320, padx=10, relief='ridge')
title_frame.grid(row=0, column=0, columnspan=2)
self.lblTitle = tk.Label(title_frame, bg='yellow', font=('times new roman', 50, 'bold'), text="Calculator Resistor", padx=200)
self.lblTitle.grid(row=0, column=0)
resistor_frame = tk.Frame(main_frame, bd=10, width=1350, padx=20, bg='dimgrey', relief='ridge')
resistor_frame.grid(row=1, column=0, sticky='w')
indicator_frame = tk.Frame(main_frame, bg='yellow', bd=10, width=1350, padx=10, relief='ridge')
indicator_frame.grid(row=1, column=1, sticky='w')
# === label Band Title ===
FONT1 = ('times new roman', 14, 'bold')
FONT2 = ('times new roman', 15, 'bold')
for i, name in enumerate(('1st', '2nd', '3rd', '4th', '5th', '6th')):
label = tk.Label(resistor_frame, text="{} Band".format(name), bg="orangered", width=8, font=FONT1)
label.grid(row=1, column=i)
#option total Band
options = {'font': 'bold', 'bg': 'dimgrey'}
self.dis = tk.Radiobutton(resistor_frame, text="3 Band", value=0, command=self.enable_band3, **options)
self.dis.grid(column=1, row=0)
self.dis4 = tk.Radiobutton(resistor_frame, text="4 Band", value=2, command=self.enable_band4, **options)
self.dis4.grid(column=2, row=0)
self.en4 = tk.Radiobutton(resistor_frame, text="5 Band", value=3, command=self.disable_band4, **options)
self.en4.grid(column=3, row=0)
self.en = tk.Radiobutton(resistor_frame, text="6 Band", value=1, command=self.disable_band3, **options)
self.en.grid(column=4, row=0)
colors_1 = (
('white', 'black'),
('black', 'peru'),
('black', 'red'),
('black', 'orange'),
('black', 'yellow'),
('black', 'green'),
('black', 'blue'),
('black', 'purple'),
('black', 'dimgrey'),
('black', 'white'),
)
colors_2 = (
('white', 'black'),
('black', 'peru'),
('black', 'red'),
('black', 'orange'),
('black', 'yellow'),
('black', 'green'),
('black', 'blue'),
('black', 'purple'),
('black', 'darkgray'),
('black', 'darkgoldenrod'),
)
# === 1st Band ===
self.buttons_band1 = []
for i, color in enumerate(colors_1):
button = tk.Button(resistor_frame, text=str(i), command=lambda x=i:self.set_band1(x), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=0)
self.buttons_band1.append(button)
# === 2nd Band ===
self.buttons_band2 = []
for i, color in enumerate(colors_1):
button = tk.Button(resistor_frame, text=str(i), command=lambda x=i:self.set_band2(x), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=1)
self.buttons_band2.append(button)
# === 3rd Band ===
self.buttons_band3 = []
for i, color in enumerate(colors_1):
button = tk.Button(resistor_frame, text=str(i), command=lambda x=i:self.set_band3(x), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=2)
self.buttons_band3.append(button)
# === 4th Band ===
values = (
('x1', 1), ('x10', 10), ('x100', 100),
('x1k', 1000), ('x10k', 10000), ('x100k', 100000),
('x1M', 1000000), ('x10M', 10000000), ('x100M', 100000000),
('x0.01', 0.01), ('x0.1M', 0.1)
)
self.buttons_band4 = []
for i, (color, value) in enumerate(zip(colors_2, values)):
if value:
button = tk.Button(resistor_frame, text=value[0], command=(lambda x=value[1]:self.set_band4(x)), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=3)
self.buttons_band4.append(button)
# === 5th Band ===
values = (
None, ('1%', 0.01), ('2%', 0.02),
None, None, ('0.5%', 0.005), ('0.25%', 0.0025), ('0.1%', 0.001),
('10%', 0.1), ('5%', 0.05),
)
self.buttons_band5 = []
for i, (color, value) in enumerate(zip(colors_2, values)):
if value:
button = tk.Button(resistor_frame, text=value[0], command=(lambda x=value[0]:self.set_band5(x)), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=4)
self.buttons_band5.append(button)
# === 6th Band ===
values = (
'250ppm/K', '100ppm/K', '50ppm/K', '15ppm/K', '25ppm/K',
)
self.buttons_band6 = []
for i, (color, value) in enumerate(zip(colors_2, values)):
if value:
button = tk.Button(resistor_frame, text=value, command=(lambda x=value:self.set_band6(x)), width=8, font=FONT1, fg=color[0], bg=color[1])
button.grid(row=i+2, column=5)
self.buttons_band6.append(button)
# === Indicator Frame ===
label = tk.Label(indicator_frame, text="Resistor Value", bg='yellow', font=FONT2)
label.grid(row=0, column=0, sticky='w', padx=10)
entry = tk.En以上是关于pycharm编译乘法计算问题?的主要内容,如果未能解决你的问题,请参考以下文章