-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_utility.py
173 lines (150 loc) · 5.47 KB
/
ui_utility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2016 Peng Liu <liupeng@imscv.com>
#
# Distributed under terms of the GNU GPL3 license.
"""
This file defines some derived UI class to help GUI works better.
"""
import Tkinter as tk
import re
import static_content
def get_country_info(list_country):
"""This function helps handle the country selected.
Keyword Arguments:
list_country -- a Tkinter.Listbox object that used to select country.
Return Value:
the selected country abbrev or raised Exception.
"""
# curselection returns str list.
country_idx = map(int, list_country.curselection())
try:
assert len(country_idx) == 1
country_idx = country_idx[0]
except AssertionError:
raise Exception("请选择国家!")
countries = sorted(static_content.get_country_dict().keys())
country = countries[country_idx]
return country
def get_name_info(entry_name):
"""This function helps handle the name typed.
Keyword Arguments:
entry_name -- a Tkinter.Entry object that used to select name.
Return Value:
the typed company name or raised Exception.
"""
name = entry_name.get()
try:
assert name != ""
assert re.match("\d", name) is None
return name
except AssertionError:
raise Exception("公司名称不能为空,或包含数字!")
def get_city_info(entry_city):
"""This function helps handle the city typed.
Keyword Arguments:
entry_city -- a Tkinter.Entry object that used to select city.
Return Value:
the typed company city or raised Exception.
"""
city = entry_city.get()
try:
assert re.match("\d", city) == None
return city
except AssertionError:
raise Exception("城市名称包含数字!")
def get_zip_info(entry_zip):
"""This function helps handle the zip typed.
Keyword Arguments:
entry_zip -- a Tkinter.Entry object that used to select zip.
Return Value:
the typed company zip or raised Exception.
"""
zip_code = entry_zip.get()
return zip_code
def get_address_info(entry_address):
"""This function helps handle the address typed.
Keyword Arguments:
entry_address -- a Tkinter.Entry object that used to select address.
Return Value:
the typed company address or raised Exception.
"""
address = entry_address.get()
return address
def get_query_info(list_country, entry_name, entry_city, entry_zip,
entry_address):
"""This function helps get the needed info for the query while raise
Exception if one of them is invalid.
Keyword Arguments:
list_country -- a Tkinter.Listbox object that used to select country.
entry_name -- a Tkinter.Entry object that used to recieve name.
entry_city -- a Tkinter.Entry object that used to recieve city.
entry_zip -- a Tkinter.Entry object that used to recieve zip_code.
entry_address -- a Tkinter.Entry object that used to recieve address.
"""
try:
country = get_country_info(list_country)
name = get_name_info(entry_name)
city = get_city_info(entry_city)
zip_code = get_zip_info(entry_zip)
address = get_address_info(entry_address)
return [country, name, city, zip_code, address]
except Exception as e:
raise Exception(e)
def update_tree_content(tree_view, result):
"""This function helps update the contents of the treeview widget, either
add new entry or update existing entry.
Keyword Arguments:
tree_view -- a ttk.Treeview object that used to display results.
result -- the result after search.
"""
for entry in result:
duns = entry["duns_number"]
country = entry["country_code"]
name = entry["name"]
city = entry["city"]
address_line1 = entry["address_line_1"]
address_line2 = entry["address_line_2"]
zip_code = entry["postal_code"]
if duns not in tree_view.get_children():
tree_view.insert("",
"end",
duns,
values=(duns, country, city, name, address_line1,
address_line2, zip_code))
def treeview_sort_column(tv, col, reverse):
"""http://stackoverflow.com/questions/22032152/python-ttk-treeview-sort-numbers
"""
l = [(tv.set(k, col), k) for k in tv.get_children('')]
l.sort(key=lambda t: str(t[0]).upper(), reverse=reverse)
# ^^^^^^^^^^^^^^^^^^^^^^^
for index, (val, k) in enumerate(l):
tv.move(k, '', index)
tv.heading(col, command=lambda: treeview_sort_column(tv, col, not reverse))
def treeview_copy_rows(tv):
"""This function helps get the curselection info to the clipboard.
Keyword Arguments:
tv --
"""
curItems = tv.selection()
output = ""
for i in curItems:
value = tv.item(i)["values"]
output += "%d|%s|%s|%s|%s\r\n" % (value[0], value[3], value[4],
value[5], value[6])
if output != "":
tv.clipboard_clear()
tv.clipboard_append(output[:-2])
def treeview_limit_entry(tv, city):
"""This function helps squeeze out those entries in the treeview whose city
isn't the same with provided.
Keyword Arguments:
tv --
city --
"""
city_upper = city.upper()
for entry in tv.get_children(''):
if city_upper not in tv.item(entry)["values"][2].upper():
tv.delete(entry)