支持将窗口作为组件引入

This commit is contained in:
gfdgd xi 2024-07-29 08:54:38 +08:00
parent 0623b95fca
commit 3bcff8dd87
3 changed files with 48 additions and 8 deletions

15
globalenv.py Normal file
View File

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
def _init():#初始化
global _global_dict
_global_dict = {}
def set_value(key,value):
""" 定义一个全局变量 """
_global_dict[key] = value
""" 获得一个全局变量,不存在则返回默认值 """
def get_value(key,defValue=None):
try:
return _global_dict[key]
except KeyError:
return defValue

View File

@ -50,8 +50,13 @@ from trans import *
from Model import *
from DefaultSetting import *
import globalenv
def PythonLower():
app = QtWidgets.QApplication(sys.argv)
if (__name__ == "__main__"):
app = QtWidgets.QApplication(sys.argv)
else:
app = globalenv.get_value("app")
QtWidgets.QMessageBox.critical(None, "错误", "Python 至少需要 3.6 及以上版本,目前版本:" + platform.python_version() + "")
sys.exit(1)
@ -2456,7 +2461,6 @@ def CheckWine():
traceback.print_exc()
except:
traceback.print_exc()
app = QtWidgets.QApplication(sys.argv)
QtWidgets.QMessageBox.critical(None, "错误", f"无法读取配置,无法继续\n{traceback.format_exc()}")
sys.exit(1)
CheckWine()
@ -2468,7 +2472,10 @@ print(wine)
###########################
# 程序信息
###########################
app = QtWidgets.QApplication(sys.argv)
if (__name__ == "__main__"):
app = QtWidgets.QApplication(sys.argv)
else:
app = globalenv.get_value("app")
trans = QtCore.QTranslator()
transeObject = QtCore.QObject()
transla = QtCore.QCoreApplication.translate
@ -3381,8 +3388,10 @@ SetFont(setting["FontSize"])
window.setCentralWidget(widget)
# 判断是否为小屏幕,是则设置滚动条并全屏
# 获取为 import 为控件,也默认开启滚动条
if (window.frameGeometry().width() > app.primaryScreen().availableGeometry().size().width() * 0.8 or
window.frameGeometry().height() > app.primaryScreen().availableGeometry().size().height() * 0.9):
window.frameGeometry().height() > app.primaryScreen().availableGeometry().size().height() * 0.9 or
__name__ != "__main__"):
# 设置滚动条
areaScroll = QtWidgets.QScrollArea(window)
areaScroll.setWidgetResizable(True)
@ -3390,9 +3399,13 @@ if (window.frameGeometry().width() > app.primaryScreen().availableGeometry().siz
areaScroll.setFrameShape(QtWidgets.QFrame.NoFrame)
window.setCentralWidget(areaScroll)
window.showMaximized() # 设置全屏
window.show()
# Mini 模式
# MiniMode(True)
sys.exit(app.exec_())
if (__name__ == "__main__"):
window.show()
# Mini 模式
# MiniMode(True)
sys.exit(app.exec_())

12
test.py Normal file
View File

@ -0,0 +1,12 @@
import sys
import globalenv
import PyQt5.QtWidgets as QtWidgets
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
globalenv._init()
globalenv.set_value("app", app)
import mainwindow
window.setCentralWidget(mainwindow.window)
window.show()
app.exec_()