Я учусь делать приложение, используя 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()