可以实现绑定箱号于装箱产品的流水号:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import os 2 import sys 3 import time 4 import openpyxl 5 import tkinter as tk 6 import tkinter.messagebox 7 from ttkbootstrap.dialogs.dialogs import * 8 import tktooltip as tips 9 10 11 def warning_prepare(text): 12 tk.messagebox.showinfo(title="警告", message="{}".format(text)) 13 14 15 def warning_gui(text): 16 Messagebox.show_info(title="警告", message="{}".format(text)) 17 18 19 def write_number(machine_number, info): 20 case_write = openpyxl.load_workbook('{}.xlsx'.format(info[2])) 21 sheet = case_write.active 22 biggest = sheet.max_row 23 for k in range(biggest - int(info[1]) + 1, biggest + 1): 24 if not sheet.cell(k, 2).value: 25 sheet.cell(k, 2).value = machine_number 26 p = k + int(info[1]) - biggest + 1 27 if k == biggest: 28 old_str = sheet.cell(biggest-int(info[1])+1, 1).value[-3:] 29 new_numb = int(old_str)+1 30 new_str = str(new_numb).zfill(3) 31 sheet.cell(biggest + 1, 1).value = info[3]+new_str 32 sheet.merge_cells('A{}:A{}'.format(biggest+1, biggest+int(info[1]))) 33 p = 1 34 break # 跳出循环、只写入一次 35 else: # 打开表格时上一箱满则创建新箱号 36 if sheet.cell(biggest, 2).value: 37 old_str = sheet.cell(biggest - int(info[1]) + 1, 1).value[-3:] 38 new_numb = int(old_str) + 1 39 new_str = str(new_numb).zfill(3) 40 sheet.cell(biggest + 1, 1).value = info[3]+new_str 41 sheet.merge_cells('A{}:A{}'.format(biggest + 1, biggest + int(info[1]))) 42 sheet.cell(biggest + 1, 2).value = machine_number 43 break 44 else: 45 continue 46 47 case_write.save('{}.xlsx'.format(info[2])) 48 return p 49 50 51 def delete_one(info): 52 def sure(): 53 xlsx_object = openpyxl.load_workbook('{}.xlsx'.format(info[2])) 54 sheet_object = xlsx_object.active 55 biggest_1 = sheet_object.max_row 56 for k in range(biggest_1 - int(info[1]) + 1, biggest_1 + 1): 57 if not sheet_object.cell(k, 2).value: 58 if k == biggest_1 - int(info[1]) + 1: 59 sheet_object.unmerge_cells('A{}:A{}'.format(biggest_1 - int(info[1]) + 1, biggest_1)) 60 sheet_object.cell(biggest_1 - int(info[1]) + 1, 1).value = None 61 sheet_object.cell(biggest_1 - int(info[1]), 2).value = None 62 sheet_object.delete_rows(biggest_1 - int(info[1])+1, biggest_1) 63 check_label.configure(text="删除成功") 64 app.update() 65 app.after(1000) 66 app.destroy() 67 break 68 else: 69 sheet_object.cell(k-1, 2).value = None 70 check_label.configure(text="删除成功") 71 app.update() 72 app.after(1000) 73 app.destroy() 74 break 75 xlsx_object.save('{}.xlsx'.format(info[2])) 76 77 def deny(): 78 app.destroy() 79 80 app = ttk.Toplevel(title="删除上条") 81 check_label = ttk.Label(app, text="确认要删除上条信息吗?") 82 check_label.grid(row=0, column=0, columnspan=2, pady=50) 83 check_button = ttk.Button(app, text="是", bootstyle="outline-toolbutton", command=sure) 84 check_button.grid(row=1, column=0, padx=20, pady=10, sticky='e') 85 deny_button = ttk.Button(app, text="否", bootstyle="outline-toolbutton", command=deny) 86 deny_button.grid(row=1, column=1, padx=20, pady=10, sticky='w') 87 app.mainloop() 88 89 90 def replace_one(info): 91 def sure(): 92 old_code = old_entry.get() 93 new_code = new_entry.get() 94 replace_xlsx = openpyxl.load_workbook('{}.xlsx'.format(info[2])) 95 replace_sheet = replace_xlsx.active 96 replace_column = replace_sheet['B'] 97 j = [] 98 for cel in replace_column: 99 j.append(cel.value) 100 if new_code in j: 101 warning_gui("重码了,请排查流水号") 102 else: 103 if old_code not in j: 104 warning_gui("需替换的条码不存在") 105 else: 106 for cel in replace_column: 107 if cel.value == old_code: 108 cel.value = new_code 109 replace_xlsx.save('{}.xlsx'.format(info[2])) 110 old_label.destroy() 111 old_entry.destroy() 112 new_label.destroy() 113 new_entry.destroy() 114 check_button.destroy() 115 deny_button.destroy() 116 success_label = ttk.Label(app_1, text='替换成功') 117 success_label.grid(row=0, column=0, pady=50, padx=50) 118 app_1.update() 119 app_1.after(1000) 120 app_1.destroy() 121 122 def deny(): 123 app_1.destroy() 124 125 app_1 = ttk.Toplevel(title="替换一条") 126 old_label = ttk.Label(app_1, text="请输入原条码:") 127 old_label.grid(row=0, column=0, padx=10, pady=10) 128 new_label = ttk.Label(app_1, text="请输入新条码:") 129 new_label.grid(row=1, column=0, padx=10, pady=10) 130 old_entry = ttk.Entry(app_1) 131 old_entry.grid(row=0, column=1, padx=10, pady=10) 132 new_entry = ttk.Entry(app_1) 133 new_entry.grid(row=1, column=1, padx=10, pady=10) 134 check_button = ttk.Button(app_1, text="确定", bootstyle="outline-toolbutton", command=sure) 135 check_button.grid(row=2, column=0, padx=20, pady=10, sticky='e') 136 deny_button = ttk.Button(app_1, text="取消", bootstyle="outline-toolbutton", command=deny) 137 deny_button.grid(row=2, column=1, padx=20, pady=10) 138 app_1.mainloop() 139 140 141 def prepare_gui(): 142 prepare = tk.Tk() 143 prepare.title("信息预设窗口") 144 prepare.geometry("410x260") 145 prepare.iconbitmap('image\\仓库.ico') 146 prepare.resizable(height=False, width=False) 147 frame_top = tk.LabelFrame(prepare, text="输入新信息开始", width=400, height=160) 148 frame_bottom = tk.LabelFrame(prepare, text='使用记录快速开始', width=400, height=80) 149 frame_top.grid(row=0, column=0, padx=5, pady=3) 150 frame_top.grid_propagate(flag=False) 151 frame_bottom.grid(row=1, column=0, padx=5, pady=3) 152 frame_bottom.grid_propagate(flag=False) 153 154 """ 定义机型 """ 155 model_label = tk.Label(frame_top, text="生产机型:") 156 model_input_box = tk.Entry(frame_top, width=15) # 输入框 157 model_label.grid(row=0, column=0, padx=5, pady=10, sticky='e') 158 model_input_box.grid(row=0, column=1, padx=5, pady=10, sticky='w') 159 160 """ 定义每箱数量""" 161 capacity_label = tk.Label(frame_top, text="每箱容量:") 162 capacity_input_box = tk.Entry(frame_top, width=15) 163 capacity_label.grid(row=0, column=2, padx=5, pady=10, sticky='e') 164 capacity_input_box.grid(row=0, column=3, columnspan=2, padx=5, pady=10, sticky='w') 165 166 """生产工单号""" 167 the_order_number_label = tk.Label(frame_top, text="生产工单:") 168 the_order_number_input_box = tk.Entry(frame_top, width=15) 169 the_order_number_label.grid(row=1, column=0, padx=5, pady=10, sticky='e') 170 the_order_number_input_box.grid(row=1, column=1, padx=5, pady=10, sticky='w') 171 tip_3 = tips.ToolTip(the_order_number_input_box, msg="此处填写的内容即为表格名称", bg='LightYellow') 172 173 """从那一箱开始绑定""" 174 the_package_label = tk.Label(frame_top, text="开始箱号:") 175 the_package_number_input_box_1 = tk.Entry(frame_top, width=9) 176 the_package_number_input_box_2 = tk.Entry(frame_top, width=4) 177 the_package_label.grid(row=1, column=2, padx=5, pady=10, sticky='e') 178 the_package_number_input_box_1.grid(row=1, column=3, padx=2, pady=10, sticky='w') 179 the_package_number_input_box_2.grid(row=1, column=4, padx=2, pady=10, sticky='w') 180 tip_1 = tips.ToolTip(the_package_number_input_box_1, msg="填箱号固定不变的部分\n没有时可不填,eg:TW-1009-", bg='LightYellow') 181 tip_2 = tips.ToolTip(the_package_number_input_box_2, msg="填箱号变化部分\neg:001", bg='LightYellow') 182 183 project_name = tk.StringVar() 184 quantity_per_case = tk.StringVar() 185 order_no = tk.StringVar() 186 package_no_fix = tk.StringVar() 187 package_no_chg = tk.StringVar() 188 189 def kill(): 190 model = model_input_box.get() # 获取输入框内容 191 capacity = capacity_input_box.get() 192 the_order_number = the_order_number_input_box.get() 193 package_1 = the_package_number_input_box_1.get() 194 package_2 = the_package_number_input_box_2.get() 195 if model == '': 196 warning_prepare("生产机型输入为空") 197 elif capacity == '': 198 warning_prepare("每箱数量输入为空") 199 elif the_order_number == '': 200 warning_prepare("生产工单输入为空") 201 elif package_2 == '': 202 warning_prepare("开始箱号输入为空") 203 else: 204 project_name.set(model) 205 quantity_per_case.set(capacity) 206 order_no.set(the_order_number) 207 package_no_fix.set(package_1) 208 package_no_chg.set(package_2) 209 prepare.after(1000) 210 prepare.destroy() 211 212 """确定按钮""" 213 sure_button_1 = tk.Button(frame_top, text="确定", command=kill, width=8) 214 sure_button_1.grid(row=2, column=0, columnspan=5, pady=5) 215 216 """查询历史记录""" 217 with open('record.py', 'r') as f: 218 record = f.readlines() 219 last = record[-1].strip('\n') 220 record_list = last.split(' ') # 将记录转化为列表 221 query_label = tk.Label(frame_bottom, text='上次记录:') 222 query_label.grid(row=0, column=0, padx=2, pady=10) 223 query_comb = tk.Label(frame_bottom, text=last) 224 query_comb.grid(row=0, column=1, padx=2, pady=10) 225 226 def kill_2(): 227 if last == '': 228 warning_prepare("无历史记录") 229 else: 230 project_name.set(record_list[0]) 231 quantity_per_case.set(record_list[1]) 232 order_no.set(record_list[2]) 233 package_no_fix.set(record_list[3]) 234 package_no_chg.set(record_list[4]) 235 prepare.after(1000) 236 prepare.destroy() 237 238 sure_button_2 = tk.Button(frame_bottom, text="确定", command=kill_2, width=8) 239 sure_button_2.grid(row=0, column=2, padx=5, pady=5) 240 241 prepare.mainloop() 242 return [project_name.get(), quantity_per_case.get(), order_no.get(), package_no_fix.get(), package_no_chg.get()] 243 244 245 def main_gui(info): 246 root = ttk.Window() 247 root.title("箱号绑定系统") # 窗口名称 248 root.geometry("820x500") 249 root.iconbitmap('image\\system.ico') # 设置窗口图标 250 root.resizable(height=False, width=False) 251 252 sample_of_code = ttk.StringVar() # 定义可变字符串 253 input_code = ttk.StringVar() 254 nnn = ttk.StringVar() 255 nnn.set('0') 256 last = ttk.StringVar() 257 last.set('') 258 temp_record = ttk.StringVar() 259 temp_record.set(info[4]) 260 261 os.chdir('report\\{}'.format(info[0])) 262 263 frame_up_left = ttk.Labelframe(root, text="预设信息区", width=500, height=148, bootstyle='info') 264 frame_right = ttk.Labelframe(root, text="信息展示区", width=300, height=400, bootstyle='primary') 265 frame_middle_left = ttk.Labelframe(root, text="信息处理区", width=500, height=248, bootstyle='warning') 266 frame_bottom = ttk.Frame(root, width=800, height=100) 267 268 frame_up_left.grid(row=0, column=0, padx=5, pady=2) 269 frame_up_left.grid_propagate(flag=False) 270 frame_right.grid(row=0, rowspan=2, column=1, padx=5, pady=2) 271 frame_right.grid_propagate(flag=False) 272 frame_middle_left.grid(row=1, column=0, padx=5, pady=2) 273 frame_middle_left.grid_propagate(flag=False) 274 frame_bottom.grid(row=2, column=0, columnspan=2, padx=5, pady=2) 275 frame_bottom.grid_propagate(flag=False) 276 277 """ 定义机型 """ 278 model_label = ttk.Label(frame_up_left, text="生产机型:" + info[0]) 279 model_label.grid(row=0, column=0, padx=35, pady=15, sticky='w') 280 281 """ 定义每箱数量""" 282 capacity_label = ttk.Label(frame_up_left, text="每箱容量:" + info[1]) 283 capacity_label.grid(row=0, column=1, padx=5, pady=15, sticky='w') 284 285 """生产工单号""" 286 the_order_number_label = ttk.Label(frame_up_left, text="订单号:" + info[2]) 287 the_order_number_label.grid(row=1, column=0, padx=35, pady=15, sticky='w') 288 289 """箱号标签""" 290 the_package_start_number = ttk.Label(frame_up_left, text="开始箱号:" + info[3] + info[4]) 291 the_package_start_number.grid(row=1, column=1, padx=5, pady=15, sticky='w') 292 293 """条码样本""" 294 sample_of_code_label = ttk.Label(frame_middle_left, text="扫描条码样本:") 295 sample_of_code_label.grid(row=0, column=0, padx=20, pady=10, sticky='e') 296 297 sample_of_code_entry = ttk.Entry(frame_middle_left) 298 sample_of_code_entry.grid(row=0, column=1, pady=10, sticky='w') 299 300 """绑定箱号输入框""" 301 input_label = ttk.Label(frame_middle_left, text="扫描车身标签:") 302 input_label.grid(row=2, column=0, padx=20, pady=10, sticky='e') 303 input_number = ttk.Entry(frame_middle_left) 304 input_number.grid(row=2, column=1, pady=10, sticky='w') 305 306 """当前箱号标签""" 307 308 present_no_label = ttk.Label(frame_middle_left, text="开始箱号:{}{}".format(info[3], info[4])) 309 present_no_label.grid(row=1, column=0, padx=20, pady=10, sticky='e') 310 311 present_storage_no = ttk.Label(frame_middle_left, text="开始默认位号:0") 312 present_storage_no.grid(row=1, column=1, padx=30, pady=10, sticky='w') 313 """按钮""" 314 delete_button = ttk.Button(frame_middle_left, text="清除上条信息", bootstyle='danger', command=lambda: delete_one(info)) 315 delete_button.grid(row=3, column=0, pady=5) 316 replace_button = ttk.Button(frame_middle_left, text="替换一条信息", bootstyle='default', command=lambda: replace_one(info)) 317 replace_button.grid(row=3, column=1, pady=5) 318 319 """信息展示区""" 320 total_number_label = ttk.Label(frame_right, text="今日入库总量:") 321 total_number_label.grid(row=0, column=0, pady=10, sticky='e') 322 323 total_number = ttk.Label(frame_right, text=nnn.get(), foreground='red') 324 total_number.grid(row=0, column=1, pady=10, sticky='w') 325 recent_label = ttk.Label(frame_right, text="最近入库{}台明细:".format(info[1])) 326 recent_label.grid(row=1, column=0, sticky='e') 327 328 recent_num = ttk.Label(frame_right, text="") 329 recent_num.grid(row=2, column=0, columnspan=2, sticky='e') 330 331 """作者标签""" 332 designer = ttk.Label(frame_bottom, text="Designer: Jianbo.Ai\nVer:2.2", font=('Arial', 10)) # 作者标签 333 designer.place(relx=0, rely=0.4) 334 335 """时间标签""" 336 begin_refresh = time.strftime("%Y/%m/%d\n%H:%M:%S") 337 show_time = ttk.Label(frame_bottom, text=begin_refresh, font=('Arial', 10)) 338 show_time.place(relx=0.88, rely=0.4) 339 340 """上一个入库条码""" 341 342 def refresh(): 343 """刷新时间""" 344 now_refresh = time.strftime("%Y/%m/%d\n%H:%M:%S") 345 show_time.configure(text=now_refresh) 346 """持续获取预设框的值""" 347 temp_code = sample_of_code_entry.get() 348 sample_of_code.set(temp_code) 349 ttt = sample_of_code.get() 350 """持续获取输入框的值""" 351 product_no = input_number.get() 352 input_code.set(product_no) 353 iii = input_code.get() 354 if not len(iii) == 0: 355 if len(iii) == len(ttt): 356 """判断是否重码""" 357 sheet = openpyxl.load_workbook('{}.xlsx'.format(info[2])).active 358 judge_repeat_column = sheet['B'] 359 j = [] 360 for col in judge_repeat_column: 361 j.append(col.value) 362 if iii in j: 363 warning_gui("重码了,请排查流水号") 364 input_number.delete('0', 'end') # 清空输入框 365 else: 366 p = write_number(iii, info) 367 nnn.set(int(nnn.get()) + 1) 368 input_number.delete('0', 'end') 369 last.set(iii) 370 work_book = openpyxl.load_workbook('{}.xlsx'.format(info[2])) 371 work_sheet = work_book.active 372 the__max__row = work_sheet.max_row 373 """列表一箱的数据""" 374 column_b = work_sheet['B'][-20:] 375 t_list = [] 376 for col_1 in column_b: 377 t_list.append(col_1.value) 378 b = list(filter(None, t_list))[-int(info[1]):] 379 recent_num.configure(text='\n'.join(b)) 380 present_no_label.configure(text="当前箱号:{}" 381 .format(work_sheet.cell(the__max__row - int(info[1]) + 1, 1).value)) 382 present_storage_no.configure(text="当前位号:{}".format(p)) 383 total_number.configure(text=nnn.get()) 384 temp_record.set(work_sheet.cell(the__max__row - int(info[1]) + 1, 1).value[-3:]) 385 root.after(1000, refresh) # 函数嵌套,形成循环 386 root.after(1000, refresh) 387 388 def save_record(): 389 a = [info[0], info[1], info[2], info[3], temp_record.get()] 390 q = ' '.join(a) 391 os.chdir('..') 392 os.chdir('..') 393 with open('record.py', 'a') as s: 394 s.write(q) 395 s.write('\n') 396 root.destroy() 397 root.protocol('WM_DELETE_WINDOW', save_record) 398 root.mainloop() # 运行窗体 399 400 401 var = prepare_gui() # 启动预设框 402 """无输入关闭预设框则退出程序""" 403 for i in range(0, 3): 404 if var[i] == "": 405 sys.exit() 406 """判断是否存在机型名称文件夹,否则创建""" 407 if not os.path.exists('report\\{}'.format(var[0])): 408 os.mkdir('report\\{}'.format(var[0])) 409 os.chdir('report\\{}'.format(var[0])) 410 workbook = openpyxl.Workbook() 411 worksheet = workbook.active 412 worksheet.cell(1, 1).value = var[3] + var[4] 413 worksheet.merge_cells('A{}:A{}'.format(1, int(var[1]))) 414 workbook.save('{}.xlsx'.format(var[2])) 415 os.chdir('..') 416 os.chdir('..') # 返回上级目录以加载图标 417 else: 418 os.chdir('report\\{}'.format(var[0])) 419 if os.path.exists('{}.xlsx'.format(var[2])): # 文件已存在则需判断输入的开始箱号是否与表格中的最后箱号一致 420 workbook = openpyxl.load_workbook('{}.xlsx'.format(var[2])) 421 worksheet = workbook.active 422 the_max_row = worksheet.max_row 423 if worksheet.cell(the_max_row - int(var[1]) + 1, 1).value == var[3] + var[4]: 424 pass 425 else: 426 warning_prepare("起始箱号异常") 427 sys.exit() 428 429 else: # 文件不存在则创建新文件并写入起始箱号,合并单元格 430 workbook = openpyxl.Workbook() 431 worksheet = workbook.active 432 worksheet.cell(1, 1).value = var[3]+var[4] 433 worksheet.merge_cells('A{}:A{}'.format(1, int(var[1]))) 434 workbook.save('{}.xlsx'.format(var[2])) 435 os.chdir('..') # 返回上级目录以加载图标 436 os.chdir('..') 437 438 main_gui(var)View Code
标签:info,箱号,column,text,frame,绑定,程序,grid,row From: https://www.cnblogs.com/abwblog/p/17293016.html