我可以在 tkinter 中使用 .config 和 Label 吗?我收到错误,而且我经验不足,所以面临解决问题

问题描述 投票:0回答:1

我收到此错误

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\\Users\\CP\\anaconda3\\Lib\\tkinter\__init_\_.py", line 1948, in __call__
return self.func(\*args)
^^^^^^^^^^^^^^^^
File "C:\\Users\\CP\\AppData\\Local\\Temp\\ipykernel_12736\\2465764817.py", line 32, in submit
result_label.config(text=f'Percentage of organic carbon: {percentage:.2f}%')
^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'config'

我试图使用 tkinter 和用户定义的函数制作土壤有机物百分比计算器。但是在我的 GUI 中显示结果时遇到问题。主要问题是 .config()

import tkinter as tk
def calculator_interface():
[root=tk.Tk(className='GUI practice')
label=tk.Label(root,text='Integrated Interface To Collect Data',font=('Times New Roman',8,'bold')).grid(row=0,column=0)
​
def submit():
b = B_Entry.get()
t = T_Entry.get()
w = W_Entry.get()
​
\# Convert values to float
b\_ = float(b)
t\_ = float(t)
w\_ = float(w)
def per_OC(b\_,t\_,w\_):
def f():
S1=1
V1=float(input("volume of K2Cr2O7(needed in blank): "))
V2=float(input('volume of FeSO4(value of blank titration): '))
​
S2=(S1\*V1)/V2
return S2
B=b\_
T=t\_
F=f()
W=w\_
C= 0.39
percentage_OC=((B-T)*F*C)/W
print('percentage_OC is ',percentage_OC)
percentage = per_OC(b\_, t\_, w\_)

        result_label.config(text=f'Percentage of organic carbon: {percentage:.2f}%')
        
        B_Entry.set("")
        T_Entry.set("")
        W_Entry.set("")

​
def tgl():
if cheak_but1.get()==1:
print('carbon analysis')
elif cheak_but2.get()==1:
print('nitrogen analysis')
else:
print('discarded')
​
B_Entry= tk.StringVar()
T_Entry= tk.StringVar()
W_Entry= tk.StringVar()
​
B_label=tk.Label(root,text='Blank',font=('Times New Roman',10,'normal')).grid(row=1,column=0)
entry1=tk.Entry(root,textvariable=B_Entry,font=('Times New Roman',10,'normal')).grid(row=1,column=1)
​
T_label=tk.Label(root,text='Titrated value',font=('Times New Roman',10,'normal')).grid(row=2,column=0)
entry2=tk.Entry(root,textvariable=T_Entry,font=('Times New Roman',10,'normal')).grid(row=2,column=1)
​
W_label=tk.Label(root,text='weight',font=('Times New Roman',10,'normal')).grid(row=3,column=0)
entry3=tk.Entry(root,textvariable=W_Entry,font=('Times New Roman',10,'normal')).grid(row=3,column=1)
​
sub_but=tk.Button(root,text='enter',command=submit).grid(row=6,column=0)
​
cheak_but1=tk.IntVar()
cheak_but2=tk.IntVar()
button1=tk.Checkbutton(root,text='carbon',variable=cheak_but1,command=tgl).grid(row=5,column=0)
button2=tk.Checkbutton(root,text='nitrogen',variable=cheak_but2,command=tgl).grid(row=5,column=1)
result_label = tk.Label(root, text='', font=('Times New Roman', 10, 'normal')).grid(row=7, column=0, columnspan=2)
​

​
root.mainloop()

calculator_interface()
​
python tkinter label
1个回答
0
投票

您将从

None
的初始定义中获取
result_label
值,因为
result_label = tk.Label(root, text='', font=('Times New Roman', 10, 'normal')).grid(row=7, column=0, columnspan=2)
意味着网格返回的值被保存为 result_label,即
None
。要解决此问题,请初始化标签并将其网格化在单独的行中,如下所示:

result_label = tk.Label(root, text='', font=('Times New Roman', 10, 'normal'))
result_label.grid(row=7, column=0, columnspan=2)
© www.soinside.com 2019 - 2024. All rights reserved.