Python GUI之Tkinter入门模板

import tkinter as tk
from tkinter import ttk
from WebHelp import *
import collections
from threading import Thread
import time
from htmlbs import *


class MainWindows(tk.Tk):

    TREESIZE = 30
    QUEDELAY = 30
    b_continueget = True
    __wh = ''
    __hb = HtmlBs()
    time_cost = 0.0
    index_list = []
    num_dict = collections.OrderedDict()
    __w = 1200
    __h = 350

    def __init__(self, path, url):
        super().__init__()  # 初始化基类
        self.title("夺金120S")
        self.geometry('1200x400')
        self.resizable(0, 0)
        self.ini_ui()
        self.__wh = WebHelp(path, url)
        self.__wh.configWeb()
        th_get = Thread(target=self.data_loop)
        th_get.setDaemon(True)
        th_get.start()

    def ini_ui(self):
        frame = tk.Frame(self, width=self.__w, height=self.__h)
        self.tree = ttk.Treeview(frame, show="headings")  # 表格
        tree_cln = ("期号", "中奖号码", "01,02", "01,03", "01,04", "01,05", "01,06", "01,07", "01,08", "01,09", "01,10", "01,11",
                                        "02,03", "02,04", "02,05", "02,06", "02,07", "02,08", "02,09", "02,10", "02,11",
                                        "03,04", "03,05", "03,06", "03,07", "03,08", "03,09", "03,10", "03,11",
                                        "04,05", "04,06", "04,07", "04,08", "04,09", "04,10", "04,11",
                                        "05,06", "05,07", "05,08", "05,09", "05,10", "05,11",
                                        "06,07", "06,08", "06,09", "06,10", "06,11",
                                        "07,08", "07,09", "07,10", "07,11",
                                        "08,09", "08,10", "08,11",
                                        "09,10", "09,11",
                                        "10,11"
        )
        self.tree["columns"] = tree_cln  # ("期号", "中奖号码", "01,02", "01,03", "01,04", "01,05", "01,06", "01,07", "01,08", "01,09", "01,10", "01,11")
        # 表示列,不显示
        for item in tree_cln:
            self.tree.column(item, width=60, anchor="center")
        self.tree.column("期号", width=80, anchor="center")
        self.tree.column("中奖号码", width=150, anchor="center")
        #  显示表头
        for item in tree_cln:
            self.tree.heading(item, text=item)

        self.tree.place(relx=0.004, rely=0.028, relwidth=0.964, relheight=0.95)
        # 给treeview添加滚动条
        self.VScroll1 = tk.Scrollbar(frame, orient='vertical', command=self.tree.yview)
        self.VScroll1.place(relx=0.971, rely=0.028, relwidth=0.014, relheight=0.958)
        self.VScroll2 = tk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)
        self.VScroll2.place(relx=0.008, rely=0.961, relwidth=0.958, relheight=0.024)
        self.tree.configure(yscrollcommand=self.VScroll1.set, xscrollcommand=self.VScroll2.set)
        # 给tree view填充初始数据
        for i in range(self.TREESIZE):
            self.tree.insert("", 0, text=str(i), values=(str(i), str(i), "3", "3", "3", "3", "3", "3", "3", "3", "3", "3"))
        frame.pack()
        self.tree.pack()
        # 添加一个button
        self.btn = tk.Button(self, text='test')
        self.btn.pack(padx=200, pady=30)
        self.btn.config(command=self.test_fun)

    def test_fun(self):
        test = {}
        for i in range(30):
            test[i] = (i + 38)
        for item in test:
            print(item)

    def get_data(self):
        tm_start = time.time()
        html = self.__wh.getPage()
        soup = self.__hb.getSoup(html)
        inlist = self.__hb.getIndex(soup)

        if inlist is not None:
            self.index_list = inlist
        nudict = self.__hb.getNum(soup)
        if nudict is not None:
            self.num_dict = nudict
        tm_end = time.time()
        self.time_cost = tm_end - tm_start
        self.__wh.reflash_web()

    def data_loop(self):
        i = 0
        while(self.b_continueget):
            i += 1
            print('loop count:%d' % i)
            self.get_data()
            print(self.index_list)
            print(self.num_dict)
            self.update_tree()
            time.sleep(self.QUEDELAY)

    def update_tree(self):
        if self.index_list is not None:
            self.edit_item()
        x = self.tree.get_children()
        # for item in x:
        #     self.tree.delete(item)
        # for i in range(self.TREESIZE):
        #     self.tree.insert("", 0, text=str(i), values=(self.index_list[i], self.num_dict[i], "3", "3", "3", "3", "3", "3", "3", "3", "3", "3"))

    def edit_item(self):
        x = self.tree.get_children()
        i = 0
        for item in x:
            if i < len(self.index_list):
                self.tree.item(item, values=(self.index_list[i], self.num_dict[i], "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"))
                i += 1


if __name__ == '__main__':
    # chrome driver路径
    path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
    # 抓取的网页url
    url = "https://ds.dsproxy.net/ds_jxf01/#/game/1-3-5"
    app = MainWindows(path, url)
    app.mainloop()

Github链接

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python GUI设计Tkinter入门到实践》是一本经典的Python GUI编程教程。该PDF由在线资源、出版社等多种方式提供下载。在这本书中,作者详细介绍了Tkinter库的基本知识和使用方法,并通过实际案例演示了GUI界面的设计和开发。 该书从Tkinter的安装开始,向读者展示了如何创建窗口、标签、按钮、文本框、列表框等常见的GUI组件。同时,作者还介绍了Tkinter的各种属性和方法,使读者可以自由地控制和定制界面的外观和行为。 除了基本的GUI组件,该书还详细介绍了Tkinter的布局管理器,如pack、grid和place,帮助读者更好地组织和布局界面元素。此外,作者还涵盖了事件处理、菜单栏、对话框、画布等高级组件的使用方法,让读者可以设计出更加复杂和丰富的GUI应用程序。 这本书的特点是结合了理论和实践,通过各种实例和项目,帮助读者逐步掌握Tkinter的使用技巧和编程思想。无论是初学者还是有一定经验的开发人员,都可以通过这本书系统地学习和掌握Python GUI编程。 总之,《Python GUI设计Tkinter入门到实践》是一本很实用的教程,通过学习它,读者可以对Tkinter有更深入的了解,并能够利用Tkinter创建出各种各样的GUI界面。无论是用于个人项目还是商业应用,都能够帮助开发者提高工作效率并设计出更加美观和易用的GUI应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值