加载中
加载中
表情图片
评为精选
鼓励
加载中...
分享
加载中...
文件下载
加载中...
修改排序
加载中...
所有教程由网友发布,仅供参考,请谨慎采纳。科创不对教程的科学性、准确性、可靠性负责。
使用Qt及python实现可视化gui设计(1)
XZH1002022/03/23原创 软件综合 IP:上海
关键词
pythonguiQt软件设计编程
pythonguiQt

在科创上许多都需要使用gui界面。虽然不知道大家用什么方式制作,但我自认为用Qt加python还是很好用的。

首先需要用到这个Qtcreator

捕获.png

打开后 文件>新建 >在左边那列选Qt然后右边选第三个点>“确定”

捕获.png

然后只要把左边那列的控件拖到中间你想要的相应位置即可

按Alt+Shift+R可以进行预览 ,全部弄完后保存得到一个 .ui 文件

最后就是将ui文件转为.py文件了。我的工具隆重登场!


Python
""" qt2py_release_v_1.3.py ui文件转Python代码 兼容PySide2 5.14.0及以下版本 """ import os import tkinter as tk import traceback from tkinter import filedialog, messagebox, ttk import PySide2 class Qt2Py:     """A class to convert ui file to Python codes."""     def __init__(self, src):         """Full directories of a ui file."""         self.src = src         self.error_dir = os.path.join(os.path.dirname(__file__), "errors.txt")     def _handle_exception(self):         """Save Exception infos to an file."""         with open(self.error_dir, 'a', encoding='utf-8'as f:             traceback.print_exc(file=f)             f.write('\n')     def _is_lower_version(self) -> bool:         """         Check if current PySide2 version is lower than 5.14.0.             :return: comparsion result         """         ps2_v, std_v = PySide2.__version__.split('.'), '5.14.0'.split('.')         len_ps2, len_std = len(ps2_v), len(std_v)         if len_ps2 > len_std:             std_v += ['0'] * (len_ps2 - len_std)         else:             ps2_v += ['0'] * (len_std - len_ps2)         for i in range(len(ps2_v)):             if int(ps2_v[i]) < int(std_v[i]):                 return True         return False     def _generate_dst(self) -> str:         """         Return a Python file name according to ui file.             :return: python file name         """         src_dir, src_name = os.path.split(self.src)         return os.path.join(src_dir, f"ui_{os.path.splitext(src_name)[0]}.py")     def _qt2py_version_higher(self) -> bool:         """         Convert higher version ui file to Python codes. Return bool.             :return: True for successful conversion False for failure.         """         # 找到并切换到PySide2所在目录         os.chdir(os.path.dirname(PySide2.__file__))         # 生成python文件名         dst = self._generate_dst()         # 开始转换         if os.system(f'.{os.sep}uic {self.src} -g python -o {dst}'):             return False         else:             try:                 with open(dst, encoding='utf-8'as f:                     codes = f.read().replace('QString()''""')                 with open(dst, 'w', encoding='utf-8'as f:                     f.write(codes)                 return True             except Exception:                 self._handle_exception()                 return False     def _qt2py_version_lower(self) -> bool:         """         Convert lower version ui file to Python codes. Return bool.             :return: True for successful conversion False for failure.         """         # 生成python文件名         dst = self._generate_dst()         import pyside2uic         # 开始转换         try:             with open(dst, 'w', encoding='utf-8'as f:                 pyside2uic.compileUi(self.src, f)         except Exception:             self._handle_exception()             return False         else:             return True     def qt2py(self) -> bool:         """         Convert ui file to Python file according PySide2 version.             :return: True for successful conversion False for failure.         """         if self._is_lower_version():             return self._qt2py_version_lower()         else:             return self._qt2py_version_higher() class Ui_Qt2Py:     """A class to create a GUI for file conversion."""     def __init__(self, master):         """Set up attributes and GUI widgets."""         self.master = master         self.setup_Master()         self.setup_Ui()         self.bind_Method()     def setup_Master(self):         """Adjustments to root window."""         self.master.resizable(FalseFalse)         width = self.master.winfo_screenwidth()         height = self.master.winfo_screenheight()         self.master.geometry(f'340x240+{(width-340)//2}+{(height-240)//2}')         self.master.title('Qt Creator转换器')     def setup_Ui(self):         """Add all widgets to root window."""         ttk.Label(             self.master, text='Qt Creator转换器', font=('Calibri'20)         ).place(anchor='center', relx=0.5, rely=0.15)         ttk.Label(             self.master, font=('Arial'12),             text=f'PySide2 版本:{PySide2.__version__}'         ).place(anchor='center', relx=0.5, rely=0.35)         self.btn_choose_file = ttk.Button(self.master, text='选择ui文件')         self.btn_choose_file.place(anchor='center', relx=0.275, rely=0.55,                                    relwidth=0.35, relheight=0.15)         self.btn_convert = ttk.Button(self.master, text='开始转换')         self.btn_convert.place(anchor='center', relx=0.725, rely=0.55,                                relwidth=0.35, relheight=0.15)         self.file_path = ttk.Label(             self.master, text='请选择需要转换的ui文件……',             font=('Arial'16), wraplength=320, anchor='center')         self.file_path.place(anchor='center', relx=0.5, rely=0.8, relwidth=1)     def _select_file(self, event=None):         """Using file-selecting dialog to get ui file path."""         file_path = filedialog.askopenfilename(             title='打开文件', filetypes=[('Qt''*.ui')])         if file_path:             self.file_path['text'] = file_path     def _convert_file(self, event=None):         """Method to convert a ui file to Python codes."""         if not os.path.isfile(self.file_path['text']):             messagebox.showerror('文件错误''请至少选择一个ui文件')         else:             q = Qt2Py(self.file_path['text'])             if q.qt2py():                 messagebox.showinfo('转换结果''转换成功')             else:                 messagebox.showinfo('转换结果''转换出错')     def _add_to_clipboard(self, event=None):         """Add file path to clipboard."""         path = self.file_path['text']         if os.path.isfile(path):             self.master.clipboard_append(path)             messagebox.showinfo('操作结果''路径已复制到剪贴板')         else:             messagebox.showinfo('操作结果''未检测到有效文件路径')     def bind_Method(self):         """Bind methods to GUI buttons."""         self.btn_choose_file['command'] = lambdaself._select_file()         self.btn_convert['command'] = lambdaself._convert_file()         self.master.bind_all('<Control-KeyPress-o>'self._select_file)         self.master.bind_all('<Control-KeyPress-e>'self._convert_file)         self.file_path.bind('<Double-Button-1>'self._add_to_clipboard) def main():     """Main entrance."""     root = tk.Tk()     Ui_Qt2Py(root)     root.mainloop() if __name__ == "__main__":     main()

打包后有点大,就把python代码放这了,大家下好相应的库就可以直接运行。

后续内容请看下回分解!😀


[修改于 3年4个月前 - 2022/03/23 14:51:02]

来自:计算机科学 / 软件综合严肃内容:教程/课程
9
3
新版本公告
~~空空如也
XZH100 作者
3年4个月前 IP:上海
902451

有发现问题一定请回复

引用
评论(1)
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论
XZH100作者
3年4个月前 IP:上海
902484
引用该用户不需要名字发表于4楼的内容
pyQT也提供了一个编译的指令,也可以实现直接把ui文件转换成py文件,我的sublime自动编译指...

好,了解了一个新知识

引用
评论
1
加载评论中,请稍候...
200字以内,仅用于支线交流,主线讨论请采用回复功能。
折叠评论

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
XZH100
进士 机友 笔友
文章
13
回复
81
学术分
0
2022/03/20注册,10时0分前活动

愚者千虑,必有一得。屡战屡败,终有一胜。

主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:上海
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的