Python Tkinter 练习2---加入Treeview来显示数据
#Time: 2020/02/16
#Author: Xiaohong
#运行环境: OS: Win7 64位 专业版Pack1
# Python: 3.7
# phantomjs: phantomjs-2.1.1-windows
# db: Python 自带的Sqlite,增加 tkinter.ttk 库
功能:在练习1的基础上,加入Treeview来显示数据
不足:1.滚动条的位置有问题还需改进 2.Treeview的抬头标题换行后如何完整显示
tk_eastmoney.py 中增加 select_data 函数,目的是从Sqlite DB中取出数据
# -*- 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! ', '是否要生成txt文件? 你输入的是:'+website)
if is_continue:
#以下,需要先在电脑上安装 phantomjs
driver = webdriver.PhantomJS(executable_path=r"E:\python_test\BS4\phantomjs-2.1.1-windows\bin\phantomjs.exe")
# driver.get('http://data.eastmoney.com/cjsj/gpkhsj.html')
driver.get(website)
time.sleep(5)
data = driver.find_element_by_xpath('//*[@id="dt_1"]').text
try:
fo = open("data.txt","w")
except IOError:
print("文件打开失败!!!")
fo.write(data)
fo.close()
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
#把同目录下的 data.txt 中 导入到 Excel 中
def toxls():
filename = get_sysdate()
# 从临时文件中读出
try:
fin = open("data.txt", "r")
except IOError:
print("Open File data.txt Failed