Python 2.7 (Python)如何组合两个程序

Python 2.7 (Python)如何组合两个程序,python-2.7,pyqt,Python 2.7,Pyqt,我想合并两个程序。我想在“第一个代码”中使用“第二个代码”。 我试过很多方法,但都失败了 作为参考,始终显示在顶部由ChristopheD编程 -----------------------------------我的节目-------------------------------------------- from PyQt4 import QtCore, QtGui import os, time, MySQLdb, socket, sys student_number = '' t

我想合并两个程序。我想在“第一个代码”中使用“第二个代码”。 我试过很多方法,但都失败了

作为参考,始终显示在顶部由ChristopheD编程

-----------------------------------我的节目--------------------------------------------

from PyQt4 import QtCore, QtGui
import os, time, MySQLdb, socket, sys



student_number = ''
t=''
t_m=''
t_day=''
t_hour=''
t_min=''
t_year=''
current_time=''



try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)





class Ui_Dialog(object):


    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(1680, 1050)
        Dialog.setMinimumSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setBold(False)
        font.setWeight(50)
        Dialog.setFont(font)
        self.pushButton = QtGui.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(1270, 830, 181, 61))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(20)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.label = QtGui.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(550, 440, 81, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(28)
        self.label.setFont(font)
        self.label.setObjectName(_fromUtf8("label"))
        self.lineEdit = QtGui.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(700, 440, 191, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(26)
        self.lineEdit.setFont(font)
        self.lineEdit.setText(_fromUtf8(""))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.label_2 = QtGui.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(480, 550, 151, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(28)
        self.label_2.setFont(font)
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.lineEdit_2 = QtGui.QLineEdit(Dialog)
        self.lineEdit_2.setGeometry(QtCore.QRect(700, 550, 191, 51))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(26)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setText(_fromUtf8(""))
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.label_3 = QtGui.QLabel(Dialog)
        self.label_3.setGeometry(QtCore.QRect(450, 190, 651, 71))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Agency FB"))
        font.setPointSize(32)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(Dialog)
        self.label_4.setGeometry(QtCore.QRect(20, 900, 211, 31))
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("Calibri"))
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setObjectName(_fromUtf8("label_4"))


        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)



        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addWidget(self.pushButton)
        Dialog.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.clickbutton)


        #self.btnExit.clicked.connect(self.close)
        #self.actionExit.triggered.connect(self.close)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
        self.pushButton.setText(_translate("Dialog", "사용시작", None))
        self.label.setText(_translate("Dialog", "학번", None))
        self.label_2.setText(_translate("Dialog", "비밀번호", None))
        self.label_3.setText(_translate("Dialog", "<html><head/><body><p><span style=\" color:#ff0000;\">아래 정보를 반드시 입력해주세요</span></p></body></html>", None))
        self.label_4.setText(_translate("Dialog", "Developed by Two PJH", None))


    def clickbutton(self):
        global student_number
        student_number = unicode(self.lineEdit.text())
        self.final()
        self.lineEdit.clear() #학번 지우기
        self.lineEdit_2.clear() #비번 지우기
        Dialog.close() #시작화면 닫고
        Dialog2.show() #종료창 열고



    def final(self):
        #global지정--------------------------------------------------------------------------
        global t, t_m, t_day, t_hour, t_min, t_year, current_time

        #컴퓨터 이름--------------------------------------------------------------------------
        computer_name = os.environ['USERNAME'] #컴퓨터에 저장된 컴퓨터이름 불러오기
        #현재 시간 받기------------------------------------------------------------------------
        t=time.localtime()
        t_m=str(t.tm_mon)
        t_day=str(t.tm_mday)
        t_hour=str(t.tm_hour)
        t_min=str(t.tm_min)
        t_year=str(t.tm_year)
        current_time=t_year+ '/' +t_m + '/' + t_day + '_' + t_hour + ':' + t_min
        #DB연동-------------------------------------------------------------------------------
        db = MySQLdb.connect(host = '203.241.227.100',  user='root', passwd='bment', charset='euckr' ,db='admin' ) #mysql 서버 접속
        cur = db.cursor() #커서를 생성하여 sql 코드 사용
        z=(computer_name, student_number, current_time) #필드별 넣을 변수 리스트화
        cur.execute("""INSERT INTO a1 VALUES(%s,%s,%s);""", z) #DB로 값 보내기
        cur.close() #생성해놓은 커서를 종료해 내용 저장
        #출력---------------------------------------------------------------------------------
        print 'Your Computer Computer_name is : %s' %computer_name #컴퓨터이름 출력
        print 'Your Computer student_number is : %s' %student_number #학번 출력
        print 'Your Computer Time is : %s' %current_time #시간 출력






class Ui_Dialog2(object):
    def setupUi(self, Dialog2):
        Dialog2.setObjectName(_fromUtf8("Dialog"))
        Dialog2.resize(120, 50)
        Dialog2.move(1530,910)
        self.pushButton = QtGui.QPushButton(Dialog2)
        self.pushButton.setGeometry(QtCore.QRect(0, 0, 120, 50))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))

        self.retranslateUi(Dialog2)
        QtCore.QMetaObject.connectSlotsByName(Dialog2)

        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addWidget(self.pushButton)
        Dialog2.connect(self.pushButton, QtCore.SIGNAL("clicked()"), self.clickbutton)

    def retranslateUi(self, Dialog2):
        Dialog2.setWindowTitle(_translate("Dialog", "End", None))
        self.pushButton.setText(_translate("Dialog", "종료", None))
        self.pushButton.setAutoDefault(False)

    def clickbutton(self):
        self.final2()
        Dialog2.close()
        Dialog.show()


    def final2(self):
        #global지정--------------------------------------------------------------------------
        #global t, t_m, t_day, t_hour, t_min, t_year, current_time
        #컴퓨터 이름--------------------------------------------------------------------------
        computer_name = os.environ['USERNAME'] #컴퓨터에 저장된 컴퓨터이름 불러오기
        #현재 시간 받기------------------------------------------------------------------------
        t2=time.localtime()
        t2_m=str(t2.tm_mon)
        t2_day=str(t2.tm_mday)
        t2_hour=str(t2.tm_hour)
        t2_min=str(t2.tm_min)
        t2_year=str(t2.tm_year)
        close_time=t2_year+ '/' +t2_m + '/' + t2_day + '_' + t2_hour + ':' + t2_min
        #DB연동-------------------------------------------------------------------------------
        db = MySQLdb.connect(host = '203.241.227.100',  user='root', passwd='bment', charset='euckr' ,db='admin' ) #mysql 서버 접속
        cur = db.cursor() #커서를 생성하여 sql 코드 사용
        z=(computer_name, student_number, current_time, close_time) #필드별 넣을 변수 리스트화
        cur.execute("""INSERT INTO a2 VALUES(%s,%s,%s,%s);""", z) #DB로 값 보내기
        cur.close() #생성해놓은 커서를 종료해 내용 저장
        #출력---------------------------------------------------------------------------------
        print 'Your Computer Computer_name is : %s' %computer_name #컴퓨터이름 출력
        print 'Your Computer student_number is : %s' %student_number #학번 출력
        print 'Your Computer Time is : %s' %current_time #시간 출력
        print 'Your Computer Close Time is : %s' %close_time #시간 출력





if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    Dialog2 = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    ui2 = Ui_Dialog2()
    ui2.setupUi(Dialog2)

    palette = QtGui.QPalette()

    palette.setBrush(QtGui.QPalette.Background,QtGui.QBrush(QtGui.QImage("background8877.jpg")))
    Dialog.setPalette(palette)

    Dialog.show()
    sys.exit(app.exec_())
import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
(如果程序2是
QMainWindow
),您可以通过设置程序2的中心小部件,将小部件从程序1放入程序2

榜样

class mymainwindow(QtGui.QMainWindow): # <- from program 2
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        .
        .
        myQDialog = QtGui.QDialog() # <- from program 1
        ui = Ui_Dialog()            # <- from program 1
        ui.setupUi(myQDialog)       # <- from program 1
        .
        .
        self.setCentralWidget(myQDialog) # <- from program 1 into 2
        .
        .

class mymainwindow(QtGui.QMainWindow):#非常感谢。但我想在“第一个代码”中使用“第二个代码”再进行一次分析..A-re????(你在开玩笑吗?)你想把“QtGui.QMainWindow”放到“QtGui.QDialog”中吗?这是不可能的(但实际上它可以但不推荐更多信息,请阅读文档参考类)。A为什么你的评论(真的)是这样的->我认为这个问题提出得很糟糕。将两个方案合并到底意味着什么,或者这一特殊情况下的实际问题在哪里,这是绝对不清楚的。