Python Tkinter 练习3--加入异常提醒
#Time: 2020/02/16
#Author: Xiaohong
#运行环境: OS: Win7 64位 专业版Pack1
# Python: 3.7
# phantomjs: phantomjs-2.1.1-windows
# db: Python 自带的Sqlite,增加 tkinter.ttk 库
功能:在练习2的基础上,加入异常提醒,并修正练习1、练习2的Bug
tk_eastmoney.py
# -*- coding: UTF-8 -*-
#Time: 2020/02/16
#Author: Xiaohong
#运行环境: OS: Win7 64位 专业版Pack1
# Python: 3.7
# phantomjs: phantomjs-2.1.1-windows
# db: Python 自带的Sqlite
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import xlwt
import tkinter as tk
import tkinter.messagebox
import sqlite3
import re
#把网址中的id为:dt_1 导入到 同目录下的 data.txt 中
def imtxt(website):
is_continue = tkinter.messagebox.askyesno('Welcome! ', '是否要生成data.txt文件? 你输入的是:'+website)
if is_continue:
try:
#以下,需要先在电脑上安装 phantomjs
driver = webdriver.PhantomJS(executable_path=r"E:\python_test\BS4\phantomjs-2.1.1-windows\bin\phantomjs.exe")
driver.get(website)
time.sleep(5)
data = driver.find_element_by_xpath('//*[@id="dt_1"]').text
with open("data.txt","w") as fo:
fo.write(data)
except:
import sys
tuple = sys.exc_info()
errmsg = ' 错误为:' + str(tuple[1])
# print(errmsg)
tkinter.messagebox.showerror(title='操作失败', message='生成data.txt文件 失败,' + errmsg)
finally:
tkinter.messagebox.showinfo(title='OK', message='生成data.txt文件 OK')
else:
tkinter.messagebox.showinfo(title='取消操作', message='您取消了导入操作')
#设立函数,来取得当前时间,作为文件名的一部分,以免文件名重复
def get_sysdate():
now = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time()))
# print(now)
return now
#把同目录下的 dat

这篇博客介绍了作者在Python Tkinter练习中如何添加异常提醒功能,该功能旨在改进之前练习1和2的错误处理,同时修复已知的Bug。内容涵盖了程序的运行环境、作者信息以及使用的技术,如Sqlite和tkinter.ttk库。
1040

被折叠的 条评论
为什么被折叠?



