def loadImage(self): print("加载图片...") if self.filename: image = Image.open(self.filename) resizeImg = self.reSize(image.size[0], image.size[1], self.imgWidth, self.imgHeight, image) return ImageTk.PhotoImage(resizeImg) else: messagebox.showinfo('提示', '未选择图片')
def saveText(self): file_text = self.text.get("1.0", "end") if file_text == '\n': messagebox.showinfo('提示', '文字框区域为空') return print("正在保存文本...") saveText_filename = filedialog.asksaveasfilename( title=u'保存文件', filetypes=[("Text Files", ".txt"), ("All Files", ".*")]) if saveText_filename[-4:] != ".txt": saveText_filename = saveText_filename + ".txt" if saveText_filename: print("保存文本内容:\n", file_text) # file_text = self.text.get("1.0", "end") try: with open(file=saveText_filename, mode='w', encoding='utf-8') as file: file.write(file_text) print("保存文本成功!") messagebox.showinfo('提示', '文件保存成功!') except Exception as e: messagebox.showinfo('提示', '文件保存失败,原因可能是{}'.format(str(e))) else: messagebox.showinfo('提示', '未选择文本名称或者文本不存在')
def handle(self, record): now = time.time() if record.levelno in self.rate_limits: rate_limit = self.rate_limits[record.levelno] else: # no limit for custom levels rate_limit = {"count": 1000, "time": 0.0} if (self.start_display_time_window is None or now - self.start_display_time_window >= rate_limit["time"]): self.count = 0 self.start_display_time_window = now if self.count < rate_limit["count"]: if tkinter_present: boxes = { logging.INFO: messagebox.showinfo, logging.WARNING: messagebox.showwarning, logging.ERROR: messagebox.showerror, logging.CRITICAL: messagebox. showerror, # Tk doesn't go any higher than error } tk = init_tkinter() boxes[record.levelno]("%s : %s" % (record.name, record.levelname), record.msg, parent=tk) elif pyqt_present: boxes = { logging.INFO: PyQt5.QMessageBox.info, logging.WARNING: PyQt5.QMessageBox.warning, logging.ERROR: PyQt5.QMessageBox.error, logging.CRITICAL: PyQt5.QMessageBox.fatal, } boxes[record.levelno](self, record.levelname, record.msg) self.count += 1 if self.count == rate_limit["count"]: t = "Limit Reached" s = "Message box limit of %d in %.1f seconds for %s reached" % ( int(rate_limit["count"]), float(rate_limit["time"]), str(record.levelname), ) if tkinter_present: tk = init_tkinter() messagebox.showinfo(t, s, parent=tk) elif pyqt_present: PyQt5.QMessageBox.info(self, t, s) self.start_display_time_window = ( now) # window is the time when the last window was closed
def openImage(self): if self.text.get(1.0, END) != '\n': question = askyesno("是否保存", "检测到文本框有内容,是否保存该内容?\n友情提醒:不保存将覆盖原有内容") if question: self.saveText() filenameTemp = filedialog.askopenfilename(title=u'选择需要识别的图片', defaultextension=".png", filetypes=[('All Files', '.*'), ('PNG', '.png'), ('JPEG', '.jpg'), ('SVG', '.svg'), ('TIFF', '.tif'), ('GIF', '.gif')]) def checkValidFormat(filename, *args): dot = '.' for ele in args: if filename.endswith(dot + ele): return True return False if filenameTemp != '': ok = checkValidFormat(filenameTemp, 'png', 'jpg', 'jpeg', 'svg', 'tif', 'tiff', 'gif') if not ok: messagebox.showinfo('提示', '请选择图片(格式支持png/jpg/svg/tif/gif)') return if self.filename != filenameTemp: self.filename = filenameTemp if self.flagShowImageLast: self.flagShowImage = False self.showImg() print('打开图片名称:', self.filename) else: # 没展现图像状态 self.startConvert() self.flagShowImage = False else: messagebox.showinfo('提示', '你选择图片和上一张重复!')
def showImg(self): if self.imgDict: self.btnShowImg.config({'state': NORMAL}) if self.flagShowImage: self.flagShowImageLast = False else: self.flagShowImageLast = True if not self.flagShowImage: if not self.filename: messagebox.showinfo('提示', '还未选择图片') return self.image = self.loadImage() self.imgZone.config({'image': self.image}) self.flagShowImage = True self.btnShowImg.config({'text': '隐藏图片'}) self.imgZone.pack(side=LEFT, fill=BOTH) self.imgZone.config(width=self.imgZoneWidth) # self.master.geometry('800x300+100+100') self.master.geometry('800x300') self.imgDict[self.filename] = self.image self.btnOpen.config({'text': '打开图片'}) self.btnConvert.config({'state': NORMAL}) return self.image else: self.flagShowImage = False self.btnShowImg.config({'text': '显示图片'}) self.imgZone.pack_forget() # self.master.geometry('400x300+100+100') self.master.geometry('400x300') self.btnOpen.config({'text': '打开图片并识别'}) self.btnConvert.config({'state': DISABLED}) return None
def startConvert(self): if self.filename: if self.filename == self.imgZongBg: messagebox.showinfo('提示', '请先选择要识别文字的图片') return print("开始识别图片") txt = self.getWord() self.btnOpen.config({'text': '请稍候,图片处理中'}) self.btnOpen.config({'state': DISABLED}) if txt: print("图片识别成功!") self.text.delete(1.0, END) self.text.insert(1.0, txt) self.btnShowImg.config({'state': NORMAL}) else: self.text.insert(1.0, '未识别到内容') self.btnOpen.config({'text': '打开图片'}) self.btnOpen.config({'state': NORMAL}) else: messagebox.showinfo('提示', '请先选择要识别文字的图片')
def click_move(self, event): x = event.x y = event.y row = 0 col = 0 if x < self.board.board_size * 50 and y < self.board.board_size * 50: for i in range(self.board.board_size): if x >= i * GRID_SIZE and x <= (i + 1) * GRID_SIZE: col = i for i in range(self.board.board_size): if y >= i * GRID_SIZE and y <= (i + 1) * GRID_SIZE: row = i if self.board.board[row][col] == 0: self.board.move(row, col) result = self.board.judge() if result == 0: if self.with_ai: self.ai_move() else: if result == -1: messagebox.showinfo('游戏结果', '游戏结束,黑子胜') if result == 1: messagebox.showinfo('游戏结果', '游戏结束,白子胜') if result == 2: messagebox.showinfo('游戏结果', '游戏结束,平局') self.load()
def ai_move(self): act = self.alpha_zero.free_play(self.board, self.alpha_zero.agent) act = self.board.action_1_to_2(act) self.board.move(act[0], act[1]) result = self.board.judge() if result == -1: messagebox.showinfo('游戏结果', '游戏结束,黑子胜') if result == 1: messagebox.showinfo('游戏结果', '游戏结束,白子胜') if result == 2: messagebox.showinfo('游戏结果', '游戏结束,平局') if result != 0: self.load()
def saveTxt(self): sfilename = asksaveasfilename(title='将图片内文字保存为文本文档', defaultextension='.txt', filetypes=[('保存文本', 'txt'), ('所有文件','*.*')]) if sfilename: stxt = self.text.get(1.0, tk.END) try: with open(sfilename, 'w') as f: f.write(stxt) messagebox.showinfo('提示', '文件保存成功') except Exception as e: #这个绝了! messagebox.showinfo('提示', '出错了,可能原因是:{}'.format(str(e))) else: messagebox.showinfo('提示','未选择文件名称,保存失败')
def change_log(self): messagebox.showinfo( "Change Log", "版本号:V0.1\n日期:2020-04-09\n开发日志:\n1. 通用文字识别\n2. 第一版界面")
def about_me(self): messagebox.showinfo( "About Me", "通用文字识别V0.1\n\n简介:基于Baidu-AIP技术开发\n作者:fanbi\n邮箱:[email protected]")