mywindow1

from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import QTimerEvent
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QMainWindow, QApplication, QMessageBox, QAction, QDialog
from mainWindow import Ui_MainWindow
from creatDom import Ui_Form
import sys
import os
from libvirt_host import Connection
from libvirt_domain import Domain
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"


class creatDom(QWidget, Ui_Form):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.move(2850, 150)
        self.setFixedSize(378, 300)

        self.pushButton.clicked.connect(self.ptn)

        self.ret = []

    def ptn(self):
        self.ret.clear()

        name = self.lineEdit.text()
        if name == '':
            QMessageBox.critical(self, 'ERROR', '请填写虚拟机名称!')
            return
        self.ret.append(name)

        v = self.lineEdit_2.text()
        if v == '' or not v.isdigit():
            QMessageBox.critical(self, 'ERROR', '请填写VCPU数量!')
            return
        self.ret.append(v)

        t = self.lineEdit_3.text()
        if not t.isdigit():
            QMessageBox.critical(self, 'ERROR', '请填写内存大小!')
            return
        self.ret.append(str(int(t) * 1024))

        self.hide()


class myWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setupUi(self)
        self.creatDom_ui = creatDom()
        self.current_nodeName = ""

        # 设置界初始界面与固定大小
        self.setFixedSize(720, 470)
        # self.center()   # 居中显示
        self.move(2850, 250)
        self.stackedWidget.setCurrentIndex(0)
        self.setWindowIcon(QIcon('320.png'))

        # 连接 KVM
        self.conn = Connection("qemu:///system")

        # 连接槽函数
        self.pushButton.clicked.connect(self.pt_clicked)
        self.pushButton_5.clicked.connect(self.pt_clicked_5)
        self.pushButton_2.clicked.connect(self.pt_clicked_2)
        self.pushButton_3.clicked.connect(self.pt_clicked_3)
        self.pushButton_4.clicked.connect(self.pt_clicked_4)
        self.action_3.triggered.connect(self.act_triggered_3)
        self.creatDom_ui.pushButton.clicked.connect(self.defineDom)
        self.action_6.triggered.connect(self.close)
        self.menufile.aboutToShow.connect(self.fileMenu)
        self.menuMPI.aboutToShow.connect(self.menumpi)
        self.pushButton_6.clicked.connect(self.pt_clicked_6)

        # 把所有虚拟机名称加载到 textEdit 中
        self.load_all_domain()

    # 启动 按钮
    def pt_clicked(self):
        node_name = self.comboBox.currentText()
        ret = self.conn.creat(node_name)
        QMessageBox.information(self, "INfo", str(ret))
        self.load_all_domain()

    # 关闭 按钮
    def pt_clicked_5(self):
        node_name = self.comboBox.currentText()
        ret = self.conn.shutdown(node_name)
        QMessageBox.information(self, "INfo", str(ret))
        self.load_all_domain()

    # 挂起 按钮
    def pt_clicked_2(self):
        node_name = self.comboBox.currentText()
        ret = self.conn.suspend(node_name)
        QMessageBox.information(self, "INfo", str(ret))
        self.load_all_domain()

    # 恢复 按钮
    def pt_clicked_3(self):
        node_name = self.comboBox.currentText()
        ret = self.conn.resume(node_name)
        QMessageBox.information(self, 'INfo', str(ret))
        self.load_all_domain()

    # 删除 按钮
    def pt_clicked_4(self):
        node_name = self.comboBox.currentText()

        ret = QMessageBox.question(self, 'Delete Node', '确定删除' + node_name + '?')
        if ret == QMessageBox.Yes:
            ret = self.conn.undefine(node_name)
            QMessageBox.information(self, 'INfo', str(ret))
            self.load_all_domain()

    # 查看 按钮
    def pt_clicked_6(self):
        ret = self.conn.getHostInfo()
        qMB = QMessageBox(self)
        qMB.setText(ret)
        qMB.setWindowTitle('主机信息')
        qMB.setFont(QFont('宋体', 12))
        qMB.show()
        # QMessageBox.information(self, '主机信息', ret)

    # 新建虚拟机
    def act_triggered_3(self):
        self.creatDom_ui.lineEdit.clear()
        self.creatDom_ui.show()

    def defineDom(self):
        ret = self.creatDom_ui.ret
        print(ret)
        res = self.conn.define(ret[0], ret[1], ret[2])
        QMessageBox.information(self, 'INfo', res)
        self.load_all_domain()

    def load_all_domain(self):
        self.textEdit.clear()
        self.current_nodeName = self.comboBox.currentText()
        self.comboBox.clear()
        self.textEdit.append('虚拟机名称\t虚拟机状态')
        # 文本框中打印所有虚拟机名称和状态, 下拉框中打印所有名字
        names, status = self.conn.get_All_domains_Names_status()
        for i in range(len(names)):
            self.textEdit.append(names[i] + '\t\t' + status[i] + '')
            self.comboBox.addItem(names[i])
        self.comboBox.setCurrentText(self.current_nodeName)

    def center(self):
        # 获取窗口大小
        screen = QtWidgets.QDesktopWidget().screenGeometry()
        size = self.geometry()

        # 本窗体运动
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)

    def fileMenu(self):
        self.stackedWidget.setCurrentIndex(0)

    def menumpi(self):
        self.stackedWidget.setCurrentIndex(1)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywindow = myWindow()
    mywindow.show()
    sys.exit(app.exec_())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艺千秋录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值