欢迎
用了蛮久的QT,突然需要使用python,哇,是真的不顺手,很难受,用的,pyqt装起来太大了,本来用python也是为了轻便嘛,唉
只能自己动手封装一个 。
- 公有:
- move
- resize
- setGeometry
- setText
- setFont
- hide
- show
- setClick
- setDbClick
- setEnable
- Lab==QLabel:
- setImg == setPixmap scaled
- setAutoImg == Qt::KeepAspectRatio
- Text==QLineEdit:
- setText
- getText == text
- Table == QTableWidget
其实就不像了,QT有QList,有QTableWidgetItem,但TK不需要呀,可以更简化,但是功能也做不到那么好了
调用的代码,做的一个登陆
以下为代码
.
from Widget import *
class Login(Widget):
def __init__(self):
super(Login, self).__init__(500, 350, "登录")
def initUI(self):
title = Lab(self.root, "登录", 0, 0, 500, 50)
title.setFont(("黑体", 36))
uidLab = Lab(self.root, '用户名:', 50, 130, 100)
pwdLab = Lab(self.root, "登录密码:", 50, 230, 100)
imgLab = Lab(self.root, '', 10, 10, 100, 100)
imgLab.setAutoImg("C:/Users/DELL/Pictures/main.ico")
self.uid = Text(self.root, 175, 130, 275)
self.pwd = Text(self.root, 175, 230, 275)
self.login = Btn(self.root, "登录", 175, 300, 150, 40, self.password)
def password(self):
uid = self.uid.getText()
pwd = self.pwd.getText()
if uid == '' or pwd == '':
return
print(uid, pwd)
if __name__ == '__main__':
login = Login()
login.start()
封装的代码,只有几个控件,Widget,Button,Label,Entry,Treeview
以下为代码
.
from tkinter.ttk import Button, Label, Entry, Treeview
from tkinter import Tk, GROOVE, StringVar
import os
from PIL import Image, ImageTk
from tkinter import messagebox
class Widget():
def __init__(self, w=1000, h=500, title='系统', x=None, y=None):
self.root = Tk()
if x == None:
self.resize(w, h)
else:
self.setGeometry(x, y, w, h)
self.root.title(title)
self.initUI()
def close(self):
self.root.destroy()
def __del__(self):
pass
def hide(self):
self.root.withdraw()
def show(self):
self.root.update()
self.root.deiconify()
def start(self):
self.root.mainloop()
def initUI(self):
pass
def setTitle(self, title):
self.root.title(title)
def resize(self, w, h):
self.width = w
self.height = h
self.root.geometry(str(w)+'x'+str(h))
def setGeometry(self, x, y, w, h):
self.width = w
self.height = h
self.root.geometry(str(w)+'x'+str(h)+'+'+str(x)+"+"+str(y))
def move(self, x, y):
self.root.geometry(str(self.width) + 'x' +
str(self.height) + '+' + str(x) + "+" + str(y))
class uiModel():
width = 0
height = 0
def move(self, x, y):
self.place(x=x, y=y)
def resize(self, w, h):
self.width = w
self.height = h
self.place(width=w, height=h)
def setGeometry(self, x, y, w, h):
self.width = w
self.height = h
self.place(x=x, y=y, width=w, height=h)
def setText(self, text):
self.configure(text=text)
def setFont(self, font=("黑体", 12)):
self.configure(font=font)
def hide(self):
self.root.withdraw()
def show(self):
self.root.update()
self.root.deiconify()
def setClick(self, func, mouse=1):
self.bind('<Double-Button-'+str(mouse)+'>', func)
def setDbClick(self, func, mouse=1):
self.bind('<Double-Button-'+str(mouse)+'>', func)
def setEnable(self, state):
if state:
self.configure(state='normal')
else:
self.configure(state='disabled')
# __init__(self,root,text='Button',x=0,y=0,w=100,h=40,command=None):
class Btn(Button, uiModel):
def __init__(self, root, text='Button', x=0, y=0, w=100, h=40, command=None):
super(Btn, self).__init__(root, text=text, command=command)
self.setGeometry(x, y, w, h)
# __init__(self,root,text=None,x=0,y=40,w=100,h=40):
class Lab(Label, uiModel):
def __init__(self, root, text, x=0, y=40, w=100, h=40):
super(Lab, self).__init__(root, text=text, img=None, anchor='center')
self.setGeometry(x, y, w, h)
def setImg(self, path=None):
if path == None:
self.configure(image=None)
self.img = None
return
pil_image = Image.open(path)
pil_image = pil_image.resize((self.width, self.height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(pil_image)
self.configure(image=img)
self.img = img
def setAutoImg(self, path):
if path == None:
self.configure(image=None)
self.img = None
return
pil_image = Image.open(path)
w = pil_image.size[0]
h = pil_image.size[1]
if w / self.width < h / self.height:
w = int(w * (self.height / h))
h = self.height
else:
h = int(h * (self.width / w))
w = self.width
pil_image = pil_image.resize((w, h), Image.ANTIALIAS)
img = ImageTk.PhotoImage(pil_image)
self.configure(image=img)
self.img = img
class Text(Entry, uiModel):
def __init__(self, root, x=0, y=80, w=100, h=40, text=None):
self.__text = StringVar()
if text != None:
self.setText(text)
super(Text, self).__init__(root, textvariable=self.__text)
self.setGeometry(x, y, w, h)
def setText(self, text):
self.__text.set(text)
def getText(self):
return self.__text.get()
class Table(Treeview, uiModel):
def __init__(self, root, x=50, y=120, w=400, h=80, columns=2, headText=['1', '2']):
self.col = columns
cs = []
for i in range(0, columns):
cs.append(str(i+1))
super(Table, self).__init__(root, show='headings', columns=cs)
for i in range(0, columns):
self.column(str(i+1), anchor='center')
self.setHeadText(i, headText[i])
self.setGeometry(x, y, w, h)
def setHeadText(self, col, text):
self.heading(str(col + 1), text=text)
def setHeadTexts(self, texts):
for i in range(0, len(texts)):
self.setHeadText(i, texts[i])
def setWidth(self, col, width):
self.column(str(col + 1), width=width)
def setWidths(self, widths):
for i in range(0, len(widths)):
self.setWidth(i, widths[i])
def getData(self):
for item in self.selection():
item_text = self.item(item, "values")
data = []
for i in range(0, self.col):
data.append(item_text[i])
return data