本博客记录与python可视化相关的库与常用操作设置等。持续更新。
1. Matplotlib
1.1. 绘制直方图
from matplotlib import pyplot
# data
print(pyplot.hist(data))
1.2. 正常显示中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
2. Tkinter
2.1. 常用tkinter框架
以下提供一个基本的tkinter框架,需要时直接复制粘贴然后完成其他开发。
# pyinstaller --noconsole test.py
import os.path
from time import strftime
from tkinter import *
from tkinter import ttk
import tkinter.font as tf
from tkinter import messagebox
from typing import Callable
class Exception_msg(Exception):
def __init__(self, message):
super().__init__(message)
class my_Entry(Entry):
def __init__(self,
root: Tk,
font: tf.Font,
hint_text: str,
default_text: str,
relx: float,
relwidth: float,
rely: float,
relheight: float,
focus_in_func: Callable[['my_Entry' or Entry, ], None],
focus_out_func: Callable[['my_Entry' or Entry, str], None], ):
super().__init__(font=font)
Label(root, text=hint_text, font=font, relief='flat', fg='black', bg='white') \
.place(relx=relx - 0.11, relwidth=relwidth - 0.2, rely=rely + 0.01, relheight=relheight - 0.02)
self.entry = Entry(root, font=font)
self.entry.insert(0, default_text)
self.entry.place(relx=relx, relwidth=relwidth, rely=rely, relheight=relheight)
self.entry.bind("<FocusIn>", lambda x: focus_in_func(self.entry))
self.entry.bind("<FocusOut>", lambda x: focus_out_func(self.entry, default_text))
def get(self) -> str:
return self.entry.get()
def insert(self, index: int, string: str) -> None:
self.entry.insert(index, string)
def clear(self, first: int, ) -> None:
self.entry.delete(first, 'end')
class my_Combobox(ttk.Combobox):
def __init__(self,
root: Tk,
values: list,
font: tf.Font,
default_text: str,
relx: float,
relwidth: float,
rely: float,
relheight: float,
func,
kwargs=None):
super().__init__(font=font, values=values)
self.box = ttk.Combobox(root, values=values, font=font)
self.box.set(default_text) # 设置默认选项
self.box.place(relx=relx, relwidth=relwidth, rely=rely, relheight=relheight)
if kwargs:
self.box.bind("<<ComboboxSelected>>", lambda x: func(self.box, **kwargs))
else:
self.box.bind("<<ComboboxSelected>>", lambda x: func(self.box, ))
def get(self) -> str:
return self.box.get()
def set(self, value) -> None:
self.box.set(value)
def set_values(self, values: list):
self.box['values'] = values
class window:
def __init__(
self,
):
self.root = Tk()
self.root.title("")
self.fonts = {'Chinese_mainBody_10': tf.Font(family='宋体', size=10),
'Chinese_mainBody_12': tf.Font(family='宋体', size=12),
'Chinese_mainBody_15': tf.Font(family='宋体', size=15),
'Chinese_mainBody_20': tf.Font(family='宋体', size=20),
'Chinese_title_12': tf.Font(family='方正小标宋简体', size=12),
'Chinese_title_15': tf.Font(family='方正小标宋简体', size=15),
'Chinese_title_20': tf.Font(family='方正小标宋简体', size=20),
'Chinese_title_25': tf.Font(family='方正小标宋简体', size=25),
'English_12': tf.Font(family='Times New Roman', size=12),
'English_15': tf.Font(family='Times New Roman', size=15),
'English_30': tf.Font(family='Times New Roman', size=30)
}
self.root.resizable(False, False)
self.root.geometry("%dx%d+%d+%d" % (300, 300, 700, 300))
self.window0()
def window0(self):
self.clear_layout()
def window1(self):
self.clear_layout()
def clear_layout(self):
for i in self.root.winfo_children():
i.destroy()
def pop_message(
self,
func: Callable[[], None],
need_quit: bool = False
):
result = messagebox.askquestion("确认", "你确定要执行操作吗?")
# 根据用户的选择执行不同的操作
if result == 'yes':
func()
self.root.quit() if need_quit else None
@staticmethod
def clear_default_text(
entry: Entry or my_Entry,
):
if isinstance(entry, Entry):
entry.delete(0, "end") # 清除默认文本
elif isinstance(entry, my_Entry):
entry.clear(0)
if __name__ == '__main__':
run = window()
run.root.mainloop()