gpt4 book ai didi

python - 从标签条目中删除索引

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:24 26 4
gpt4 key购买 nike

代码从用户那里获取星期输入,在 Excel 文件中搜索包含该星期的行,将结果写入到不同的 Excel 文件中,然后将列写入 tkinter 标签上。问题是 GUI(标签)上显示索引值。我如何从 GUI(标签)隐藏/删除此索引值

Picture of GUI

import tkinter as tk
from datetime import date, datetime
from tkinter import *
from tkinter import font
from warnings import simplefilter
import openpyxl
import pandas as pd
from openpyxl import Workbook
from openpyxl.styles import Alignment
import xlrd
from pandas import ExcelWriter

simplefilter("ignore")
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np


wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb["Productie"]
week_number = date.today().isocalendar()[1]

root = tk.Tk()
root.title("Main Menu")
root.geometry("1000x500")
fontmare = font.Font(family='Helvetica', size=14, weight='bold')
var = tk.StringVar()
def exportexcel():
wb_sales = pd.ExcelFile("excel.xlsx")
sheet_3 = pd.read_excel("excel.xlsx", sheet_name= 2,dtype=str)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 500)
read_sheets_name = wb_sales.sheet_names
weekenter = var.get()
print(weekenter)
print(sheet_3.query("['6'] in Week"))
global sheetweek
if weekenter == "6":
#sheetweek.set(sheet_3.query("['6'] in Week"))
writer = ExcelWriter('weekexport.xlsx')
sheet_3.query("['6'] in Week").to_excel(writer, 'Foaie1',index=False,)
writer.save()
print(sheet_3.query("['6'] in Week"))
if weekenter == "7":
#sheetweek.set(sheet_3.query("['7'] in Week"))
writer = ExcelWriter('weekexport.xlsx')
sheet_3.query("['7'] in Week").to_excel(writer, 'Foaie1',index=False,)
writer.save()
print(sheet_3.query("['7'] in Week"))

#####################################################################################################
global dfM
global dfB
global dfQ
global dfSN1
global dfSN2
dfM.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="D",index_col=None))
dfB.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="E",index_col=None))

dfQ.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="F",index_col=None))

dfSN1.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="G",index_col=None))

dfSN2.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="H",index_col=None))








root.mainloop()



def write_to_xlsx():
wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb["Productie"]
week_number = date.today().isocalendar()[1]
ws = wb.active
maxim = ws.max_row + 1

for i in range(8):
ws.merge_cells(start_row=maxim, start_column=i+1, end_row=maxim + 1, end_column=i+1) # A

ws.merge_cells(start_row=maxim, start_column=24, end_row=maxim + 1, end_column=24) # X
ws.merge_cells(start_row=maxim, start_column=25, end_row=maxim + 1, end_column=25) # Y
week_number = date.today().isocalendar()[1]
ws.cell(column=1, row=maxim, value=week_number)

now = datetime.now()
dt_string = now.strftime('%d/%m')
ws.cell(column=2, row=maxim, value=dt_string)

for ndex, entry in enumerate(entry_list):
ws.cell(column=ndex+3, row=maxim, value=entry_list[ndex].get())
entry_list[ndex].delete(0, 'end')

rows = range(1, maxim + 1)
columns = range(1, 8)
for row in rows:
for col in columns:
sheet.cell(row, col).alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)

wb.save("excel.xlsx")


label_list = ['Model', 'Etapa', 'Batch', 'Cantitate', 'S/N']
entry_list = []

for i in range(len(label_list)):
tk.Label(root, text=label_list[i]).grid(row=i, column=0, sticky='w')
entry_list.append(tk.Entry(root, width=10))
entry_list[-1].grid(row=i, column=1, sticky='w',)
dfM = StringVar()
dfB = StringVar()
dfQ = StringVar()
dfSN1 = StringVar()
dfSN2 = StringVar()
dfM.set("Model")
dfB.set("Batch")
dfQ.set("Cantitate")
dfSN1.set("Serial")
dfSN2.set("Number")
tk.Button(root, text='Submit', command=write_to_xlsx).grid(row=2, column=2, sticky='W')
tk.Button(root, text='Week:', command=exportexcel).grid(row=6, column=0, sticky='W')
entry = tk.Entry(root, textvariable=var, width=10).grid(row=7, column=0, sticky='w')
LabelModel = tk.Label(root, textvariable=dfM, font=fontmare).grid(row=8, column=0,sticky="w")
LabelBatch = tk.Label(root, textvariable=dfB, font=fontmare).grid(row=8, column=1,sticky="w")
LabelQty = tk.Label(root, textvariable=dfQ, font=fontmare).grid(row=8, column=2,sticky="w")
LabelSerial = tk.Label(root, textvariable=dfSN1, font=fontmare).grid(row=8, column=3,sticky="w")
LabelNumber = tk.Label(root, textvariable=dfSN2, font=fontmare).grid(row=8, column=4,sticky="w")



root.mainloop()

最佳答案

我想做很多事情,但今天时间不多了。

根据 this post 似乎有一个关于 index_col=None 问题的错误报告.

相关错误报告:GH18792GH20480

为了解决这个问题,我所做的是将数据转换为字符串,然后将其分解为各个组成部分。一旦我有了这些,我就可以再次运行 split() 来删除索引。

目前,这是您正在寻找的修复方法:

替换:

global dfM
global dfB
global dfQ
global dfSN1
global dfSN2
dfM.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="D",index_col=None))
dfB.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="E",index_col=None))
dfQ.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="F",index_col=None))
dfSN1.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="G",index_col=None))
dfSN2.set(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1', header=None, usecols="H",index_col=None))

与:

var_lst = [dfM, dfB, dfQ, dfSN1, dfSN2]
for ndex, i in enumerate(['D', 'E', 'F', 'G', 'H']):
final_string = ''
build_list = str(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1',
header=None, usecols=i, index_col=None)).split('\n')
for string in build_list:
x = ' '.join(string.split()).split()
if len(x) > 1:
final_string = '{} {}\n'.format(final_string, ' '.join(string.split()).split()[1])
var_lst[ndex].set(final_string)

结果:

enter image description here

进一步审查:

您可能想要更改一些内容。例如,如果需要,您可以在一行上使用 global all。 global var1, var2, var3 ... 然而,您的代码中根本不需要 global 。

您已经动态构建了条目信息,因此让我们对标签和其他变量执行相同的操作。

我还注释掉了一些根本没有使用的行,并且您的代码似乎仍在工作,因此如果您有任何问题,请告诉我。

我删除了未使用的导入,并对代码进行了一些常规清理,以更紧密地遵循 PEP8。

修改后的代码:

import tkinter as tk
from tkinter import font
from datetime import date, datetime
from openpyxl.styles import Alignment
from warnings import simplefilter
from pandas import ExcelWriter
import pandas as pd
import openpyxl


def export_excel():
# wb_sales = pd.ExcelFile("excel.xlsx") # line not used
sheet_3 = pd.read_excel("excel.xlsx", sheet_name=2, dtype=str)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 500)
# read_sheets_name = wb_sales.sheet_names # line not used
weekenter = var.get()
if weekenter == "6":
writer = ExcelWriter('weekexport.xlsx')
sheet_3.query("['6'] in Week").to_excel(writer, 'Foaie1', index=False)
writer.save()
if weekenter == "7":
writer = ExcelWriter('weekexport.xlsx')
sheet_3.query("['7'] in Week").to_excel(writer, 'Foaie1', index=False)
writer.save()

for ndex, i in enumerate(['D', 'E', 'F', 'G', 'H']):
final_string = ''
build_list = str(pd.read_excel('weekexport.xlsx', sheet_name='Foaie1',
header=None, usecols=i, index_col=None)).split('\n')
for string in build_list:
x = ' '.join(string.split()).split()
if len(x) > 1:
final_string = '{} {}\n'.format(final_string, ' '.join(string.split()).split()[1])
var_list[ndex].set(final_string)


def write_to_xlsx():
wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb["Productie"]
# week_number = date.today().isocalendar()[1] # line not used.
ws = wb.active
maxim = ws.max_row + 1

for i in range(8):
ws.merge_cells(start_row=maxim, start_column=i+1, end_row=maxim + 1, end_column=i+1) # A

ws.merge_cells(start_row=maxim, start_column=24, end_row=maxim + 1, end_column=24) # X
ws.merge_cells(start_row=maxim, start_column=25, end_row=maxim + 1, end_column=25) # Y
week_number = date.today().isocalendar()[1]
ws.cell(column=1, row=maxim, value=week_number)

now = datetime.now()
dt_string = now.strftime('%d/%m')
ws.cell(column=2, row=maxim, value=dt_string)

for ndex, entry in enumerate(entry_list):
ws.cell(column=ndex+3, row=maxim, value=entry_list[ndex].get())
entry_list[ndex].delete(0, 'end')

rows = range(1, maxim + 1)
columns = range(1, 8)
for row in rows:
for col in columns:
sheet.cell(row, col).alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
wb.save("excel.xlsx")


root = tk.Tk()
root.title("Main Menu")
root.geometry("1000x500")
font_mare = font.Font(family='Helvetica', size=14, weight='bold')
var = tk.StringVar()
simplefilter("ignore")
wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb["Productie"]
week_number = date.today().isocalendar()[1]

label_list = ['Model', 'Etapa', 'Batch', 'Cantitate', 'S/N']
entry_list = []

for i in range(len(label_list)):
tk.Label(root, text=label_list[i]).grid(row=i, column=0, sticky='w')
entry_list.append(tk.Entry(root, width=10))
entry_list[-1].grid(row=i, column=1, sticky='w')

tk.Button(root, text='Submit', command=write_to_xlsx).grid(row=2, column=2, sticky='W')
tk.Button(root, text='Week:', command=export_excel).grid(row=6, column=0, sticky='W')
entry = tk.Entry(root, textvariable=var, width=10).grid(row=7, column=0, sticky='w')

label_list_2 = ['Model', 'Batch', 'Cantitate', 'Serial', 'Number']
var_list = []
for ndex, i in enumerate(label_list_2):
var_list.append(tk.StringVar(root))
var_list[-1].set(i)
tk.Label(root, textvariable=var_list[-1], font=font_mare).grid(row=8, column=ndex, sticky="w")

root.mainloop()

关于python - 从标签条目中删除索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60402291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com