zoukankan      html  css  js  c++  java
  • python学习笔记(Tkinter编程利用Treeview实现表格自动更新)

    博主今天总结这段时间抽空写的一个GUI编程项目

    功能是查看本地打印机队列,可选择指定队列重新打印

    直接上图

    UI设计包括3个区域左上方,右上方和下方列表区域

    使用网格grid方法来分配位置

    下面是界面设计的代码

      1 #!/usr/bin/env python
      2 # -*- coding: utf-8 -*-
      3 
      4 from Tkinter import *
      5 from printer_pywin32 import PrinterPywin32
      6 import ttk
      7 
      8 
      9 class PrinterTkinter:
     10     def __init__(self):
     11         self.root = Tk()
     12         self.root.title("打印机监控系统")
     13 
     14         self.frame_left_top = Frame(width=400, height=200)
     15         self.frame_right_top = Frame(width=400, height=200)
     16         self.frame_center = Frame(width=800, height=400)
     17         self.frame_bottom = Frame(width=800, height=50)
     18 
     19         # 定义左上方区域
     20         self.left_top_title = Label(self.frame_left_top, text="打印状态:", font=('Arial', 25))
     21         self.left_top_title.grid(row=0, column=0, columnspan=2, sticky=NSEW, padx=50, pady=30)
     22 
     23         self.var_success = StringVar()  # 声明成功数
     24         self.var_false = StringVar()    # 声明失败数
     25 
     26         self.left_top_frame = Frame(self.frame_left_top)
     27         self.left_top_frame_left1 = Label(self.frame_left_top, text="打印成功数", font=('Arial', 20))
     28         self.left_top_frame_left2 = Label(self.frame_left_top, textvariable=self.var_success, font=('Arial', 15))
     29         self.get_success()  # 调用方法更新成功数
     30         self.left_top_frame_right1 = Label(self.frame_left_top, text="打印失败数", font=('Arial', 20))
     31         self.left_top_frame_right2 = Label(self.frame_left_top, textvariable=self.var_false, font=('Arial', 15))
     32         self.get_false()    # 调用方法更新失败数
     33         self.left_top_frame_left1.grid(row=1, column=0)
     34         self.left_top_frame_left2.grid(row=1, column=1)
     35         self.left_top_frame_right1.grid(row=2, column=0)
     36         self.left_top_frame_right2.grid(row=2, column=1)
     37 
     38         # 定义右上方区域
     39         self.var_entry = StringVar()
     40 
     41         self.right_top_title = Label(self.frame_right_top, text="重新打印的任务编号:", font=('Arial', 20))
     42         self.right_top_entry = Entry(self.frame_right_top, textvariable=self.var_entry)
     43 
     44         self.number = int
     45         self.right_top_button = Button(self.frame_right_top, text="确定", command=self.button_restart, font=('Arial', 15))
     46         self.right_top_title.grid(row=0, column=0)
     47         self.right_top_entry.grid(row=1, column=0)
     48         self.right_top_button.grid(row=2, column=0, padx=20, pady=20)
     49 
     50         # 定义中心列表区域
     51         self.tree = ttk.Treeview(self.frame_center, show="headings", height=18, columns=("a", "b", "c", "d", "e"))
     52         self.vbar = ttk.Scrollbar(self.frame_center, orient=VERTICAL, command=self.tree.yview)
     53         # 定义树形结构与滚动条
     54         self.tree.configure(yscrollcommand=self.vbar.set)
     55 
     56         # 表格的标题
     57         self.tree.column("a", width=50, anchor="center")
     58         self.tree.column("b", width=200, anchor="center")
     59         self.tree.column("c", width=200, anchor="center")
     60         self.tree.column("d", width=100, anchor="center")
     61         self.tree.column("e", width=150, anchor="center")
     62         self.tree.heading("a", text="编号")
     63         self.tree.heading("b", text="打印时间")
     64         self.tree.heading("c", text="打印名称")
     65         self.tree.heading("d", text="打印任务编号")
     66         self.tree.heading("e", text="打印状态")
     67 
     68         # 调用方法获取表格内容插入
     69         self.get_tree()
     70         self.tree.grid(row=0, column=0, sticky=NSEW)
     71         self.vbar.grid(row=0, column=1, sticky=NS)
     72 
     73         # 整体区域定位
     74         self.frame_left_top.grid(row=0, column=0, padx=2, pady=5)
     75         self.frame_right_top.grid(row=0, column=1, padx=30, pady=30)
     76         self.frame_center.grid(row=1, column=0, columnspan=2, padx=4, pady=5)
     77         self.frame_bottom.grid(row=2, column=0, columnspan=2)
     78 
     79         self.frame_left_top.grid_propagate(0)
     80         self.frame_right_top.grid_propagate(0)
     81         self.frame_center.grid_propagate(0)
     82         self.frame_bottom.grid_propagate(0)
     83 
     84         self.root.mainloop()
     85 
     86     # 得到打印成功数
     87     def get_success(self):
     88         self.var_success.set(PrinterPywin32.get_success())
     89         self.left_top_frame_left2.after(500, self.get_success)
     90 
     91     # 得到打印失败数
     92     def get_false(self):
     93         self.var_false.set(PrinterPywin32.get_false())
     94         self.left_top_frame.after(500, self.get_false)
     95 
     96     # 表格内容插入
     97     def get_tree(self):
     98         # 删除原节点
     99         for _ in map(self.tree.delete, self.tree.get_children("")):
    100             pass
    101         # 更新插入新节点
    102         for i in range(len(PrinterPywin32.get_enumjobs())):
    103             self.tree.insert("", "end", values=(i + 1, PrinterPywin32.get_enumjobs()[i]["Submitted"],
    104                                                 PrinterPywin32.get_enumjobs()[i]["pPrinterName"],
    105                                                 PrinterPywin32.get_enumjobs()[i]["JobId"],
    106                                                 PrinterPywin32.get_enumjobs()[i]["Status"]))
    107         self.tree.after(500, self.get_tree)
    108 
    109     # 重新打印
    110     def button_restart(self):
    111         self.number = self.right_top_entry.get()
    112         PrinterPywin32.printer_restart(self.number)
    113 
    114 
    115 if __name__ == '__main__':
    116     PrinterTkinter()

    这里实现了Label控件标题文字的定时更新与表格内容的自动更新

  • 相关阅读:
    STL hash_map使用
    STL的 string 类赋值
    STL map使用详解
    下面我使用vector容器为基础来构成一棵树
    MFC中CString.Format的详细用法
    error LNK2001: 无法解析的外部符号 "public: static class stdext::hash_map
    !!! STL的string类如何实现CString的Format功能 这是一个经典问题,记住
    STL map和STL set(转载)
    为什么提示此错误?RunTime Check Failure #2 Stack around the variable 'tch1'was corrupted.
    STL源码剖析
  • 原文地址:https://www.cnblogs.com/cllovewxq/p/5776346.html
Copyright © 2011-2022 走看看