如何在pycharm python上从3到6按钮进行乘法和求和
我正在学习使用python和tkinter制作应用程序。 我做了一个电阻计算器。问题是如何计算几个按钮的乘法和加法以获得值,并将总结果写入电阻器值输入框 更糟糕的是,我不知道如何在这里编写代码。所以我给了链接 多谢各位如何在pycharm python上从3到6按钮进行乘法和求和,python,tkinter,Python,Tkinter,我正在学习使用python和tkinter制作应用程序。 我做了一个电阻计算器。问题是如何计算几个按钮的乘法和加法以获得值,并将总结果写入电阻器值输入框 更糟糕的是,我不知道如何在这里编写代码。所以我给了链接 多谢各位 在函数SetBandaftervarX.set()中,您可以执行计算值并在条目中更新值的函数 其他更改(减少了代码行): 我在列表中保留颜色和按钮文本,以便使用for-loop(使用zip()和enumerate())生成按钮。我将按钮保留在列表中,以便以后可以使用for-l
在函数
SetBand
aftervarX.set()
中,您可以执行计算值并在条目中更新值的函数
其他更改(减少了代码行): 我在列表中保留颜色和按钮文本,以便使用
for
-loop(使用zip()
和enumerate()
)生成按钮。我将按钮保留在列表中,以便以后可以使用for
-loop来更改按钮中的状态
在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.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_resistance)
entry.grid(row=0, column=1, pady=3, columnspan=3)
label = tk.Label(indicator_frame, text="Tolerance", bg='yellow', font=FONT2)
label.grid(row=1, column=0, sticky='w', padx=10)
entry = tk.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_tolerance)
entry.grid(row=1, column=1, pady=10, columnspan=3)
label = tk.Label(indicator_frame, text="Temp. Coefficient", bg='yellow', font=FONT2)
label.grid(row=2, column=0, sticky='w', padx=10)
entry = tk.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_temp)
entry.grid(row=2, column=1, pady=10, columnspan=5)
button = tk.Button(indicator_frame, text="QUIT", command=self.quit, width=8, height=2)
button.grid(column=1, row=3, pady=10)
button = tk.Button(indicator_frame, text="RESET", command=self.reset, width=8, height=2)
button.grid(column=2, row=3, pady=10)
button = tk.Button(indicator_frame, text="HITUNG", command=self.hitung, width=8, height=2)
button.grid(column=3, row=3, pady=10)
def enable_band3(self):
if self.en == 0:
value = 'normal'
else:
value = 'disabled'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def disable_band3(self):
if self.dis != 0:
value = 'normal'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def enable_band4(self):
if self.en4 != 1:
value = 'normal'
for button in self.buttons_band5:
button.configure(state=value)
value = 'disable'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def disable_band4(self):
if self.dis4 != 1:
value = 'normal'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
value = 'disable'
for button in self.buttons_band6:
button.configure(state=value)
if __name__=='__main__':
root = tk.Tk()
application = Resistor(root)
root.mainloop()
顺便说一句:如果我将
IntVar
保留在列表中,那么我可以使用一个set\u波段(self,number,value)
而不是set\u波段1(self,value)
,set\u波段2(self,value)
,等等。如果你想让人们帮助你,请学习如何在这里发布,因为人们不会倾向于点击随机链接。将代码放入问题中,并使用按钮{}
设置格式。发布相关代码,突出显示它,当你开始一次又一次地给变量名添加数字时,按下Ctrl+K
,就会出现问题,你应该使用适当的数据结构(列表、字典等)。你应该在列表上保留所有IntVar()
和按钮,然后你可以使用for
-循环来缩短代码。谢谢你的回答。但是如何将值ex:1000格式化为1k等。如果rad.button 3处于活动状态,如何将值v1从*1000更改为*10要将1000
格式化为1k
必须手动执行-除以1000,结果转换为字符串并添加字符k
。如果只想转换大于1000的值,请使用If/else
-ie.If-value>=1000:。当您想要转换到
1M时类似。首先检查
if value>=1000000`,然后除以1 000 000
,将结果转换为字符串并添加字符M
,如果您想在按钮3处于活动状态时使用*1000而不是*10,那么当您在更新标签
no.1中计算值时,请使用if/elif/else
。我还是不知道如何转换,第二,乘法不变
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.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_resistance)
entry.grid(row=0, column=1, pady=3, columnspan=3)
label = tk.Label(indicator_frame, text="Tolerance", bg='yellow', font=FONT2)
label.grid(row=1, column=0, sticky='w', padx=10)
entry = tk.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_tolerance)
entry.grid(row=1, column=1, pady=10, columnspan=3)
label = tk.Label(indicator_frame, text="Temp. Coefficient", bg='yellow', font=FONT2)
label.grid(row=2, column=0, sticky='w', padx=10)
entry = tk.Entry(indicator_frame, width=16, font=FONT2, textvariable=self.var_temp)
entry.grid(row=2, column=1, pady=10, columnspan=5)
button = tk.Button(indicator_frame, text="QUIT", command=self.quit, width=8, height=2)
button.grid(column=1, row=3, pady=10)
button = tk.Button(indicator_frame, text="RESET", command=self.reset, width=8, height=2)
button.grid(column=2, row=3, pady=10)
button = tk.Button(indicator_frame, text="HITUNG", command=self.hitung, width=8, height=2)
button.grid(column=3, row=3, pady=10)
def enable_band3(self):
if self.en == 0:
value = 'normal'
else:
value = 'disabled'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def disable_band3(self):
if self.dis != 0:
value = 'normal'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def enable_band4(self):
if self.en4 != 1:
value = 'normal'
for button in self.buttons_band5:
button.configure(state=value)
value = 'disable'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band6:
button.configure(state=value)
def disable_band4(self):
if self.dis4 != 1:
value = 'normal'
for button in self.buttons_band3:
button.configure(state=value)
for button in self.buttons_band5:
button.configure(state=value)
value = 'disable'
for button in self.buttons_band6:
button.configure(state=value)
if __name__=='__main__':
root = tk.Tk()
application = Resistor(root)
root.mainloop()