python - 我的条目的 stringvar 无法正常工作
问题描述
我有以下问题:我有一张表格,其中记录了需要哪个标签的文章。更改条目时应自动更新所需的标签。我在数据框中查找文章编号,然后将需要的标签作为图片返回。
出于某种原因,它只能工作一次,并且只有当我输入复制并粘贴正确的文章编号时。在第一次之后总是将 exept 值设置为我的变量。
我也不明白 *args 作为我的 search_label 函数的输入的含义。但是没有它,我在输入字段中输入内容后立即给了我错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\Python_3.9.1\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
TypeError: search_label() takes 0 positional arguments but 3 were given
from multiprocessing import Process
import tkinter as tk
import pandas as pd
from PIL import ImageTk, Image
import os
import tkinter.messagebox as tkmb
def display_img(img_src):
global img
img = Image.open(img_src)
'size of the picture'
width_hight = 180
img = img.resize((width_hight, width_hight))
img = ImageTk.PhotoImage(img)
panel = tk.Label(root, image=img, bd=0)
'place in the middle of the window'
panel.place(x=width / 2 - width_hight / 2, y=row3)
def search_label(*args):
try:
artikel = entry_var.get()
artikel = str(artikel).upper()
print('entry= ', artikel)
#Needed cols of Excelfile
cols = ['Nr_','Reinigungsmittel']
global df
df = pd.DataFrame(df, columns=cols)
#For some reason there are always spaces at the end of the artikel in the dataframe
#remove spaces
df['Nr_'] = df['Nr_'].str.strip()
df.to_csv('Reinigungsmittel.csv', sep= ";", encoding='utf-8-sig')
df = df.set_index('Nr_')
#filter the dataframe only for artikel number
print('df:')
print(df)
#make entry capital letters
df_reinigungsmittel = df.loc[[artikel]]
#get only the cleanning article
cleaning_article = str(df_reinigungsmittel.iloc[0][0])
print('ergebnis:')
print(cleaning_article)
cleaning_article = str(cleaning_article).strip()
except:
cleaning_article = 'label search failed'
evaluation(cleaning_article)
def evaluation(cleaning_article):
print('input evaluation:',cleaning_article)
if cleaning_article == 'CMETHANOL':
cleaning_article = 'rot.png'
elif cleaning_article == 'CPROPANOL2':
cleaning_article = 'gruen.png'
elif cleaning_article == 'CPROPANOL':
cleaning_article = 'blau.png'
else:
cleaning_article = 'non.png'
print(cleaning_article)
display_img(cleaning_article)
#load dataframe when program is opend, so the dataframe doent' need to be loaded for each search
datasource = r'C:\Arbeit\scripte\Reinigungslabel\Reinigungslabel.xlsx'
datasource_csv = r'C:\Arbeit\scripte\Reinigungslabel\Reinigungsmittel.csv'
#df = pd.read_excel(datasource)
df = pd.read_csv(datasource_csv, sep =';')
# Window and layout settings
hight = 270
width = 250
column_space = 5
column1 = 10
width_column1 = width - 20
#start value of cleaning_article
cleaning_article = 'non.png'
hight_row = 30
space_row = 5
row1 = 10
row2 = row1 + hight_row + space_row
row3 = row2 + hight_row + space_row
root = tk.Tk()
entry_var = tk.StringVar()
entry_var.trace('w',search_label)
print(df)
root.title("Reinigungs Label V1.0")
# get screen width and height
# width of the screen
ws = root.winfo_screenwidth()
# height of the screen
hs = root.winfo_screenheight()
# calculate x and y coordinates for the Tk root window
x = ws - (1.05 * width)
y = 30
root.geometry('%dx%d+%d+%d' % (width, hight, x, y))
root.configure(bg='black', bd=0)
Canvas = tk.Canvas(root, height=hight, width=width).place()
frame_artikel = tk.Frame(root, bg = 'black')
frame_artikel.place(x=column1, y=row1, width=width_column1, height=hight_row)
entry_artikel = tk.Entry(frame_artikel, textvariable=entry_var, font=20)
entry_artikel.focus()
entry_artikel.place(relwidth=1, relheight=1)
root.mainloop()
解决方案
推荐阅读
- c++ - Arduino Nano 33 IoT & NEO 6M GPS 模块
- three.js - 从 Blender 到三个 js 的 3d 模型遇到问题
- statistics - 我们如何对连续 X 和 Y 变量进行假设检验
- c# - 可以使用 Json.net 将自定义序列化程序放入被序列化的对象中吗?
- javascript - 使用javascript将多个css-property box-shadowes添加到一个元素
- node.js - 如何让我的 User.findByIdAndUpdate 仅在密码设置为我的新密码后运行?
- r - 在绘图标题中组合文本和数学符号
- java - 在 Eclipse 中安装 glassfish 时出错
- python - 如何从 python 中的另一个外部文件引用 python 类中函数内的变量?
- python - 如何将jupyter notebook中的动图保存到pdf中?