2

我正在尝试将下面的代码(位于 中tkinter)迁移到pyqt. 我很陌生pyqt。我无法得到太多细节。

import os, sys, time
import os.path
import shutil
import sys
import Tkinter as tk
from Tkinter import *
root = Tk()
frame = Frame(root)

def enable_():
    # Global variables
    if (var.get()==1):
        E5.config(state=NORMAL)
        E5.insert(0, 1)
        for c in checkbuttons:          
            c.config(state=NORMAL)  
    # disable test selection
    else:
        E5.delete(0, END)
        E5.config(state=DISABLED)
        for c in checkbuttons:          
            c.config(state=DISABLED)

# Select geometry
root.geometry("500x570+500+300")
root.title('Test Configure')
# Global variable declaration
global row_me
global col_me
row_me =9
col_me =0
L1 = Label ().grid(row=0,column=1)  
Label (text='Target IP Address').grid(row=1,column=0)
E4 = Entry(root, width=20)
E4.grid(row=1,column=1)
L1 = Label ().grid(row=2,column=1)
variable = StringVar()
# Check button creation
var = IntVar()
var_mail = IntVar()
# Create check buttons
R1=Checkbutton(root, text = "Enable Test Suite Selection", variable = var, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20, command=enable_)
R1.grid(row=3,column=1)
R2=Checkbutton(root, text = "Send Email", variable = var_mail, \
                 onvalue = 1, offvalue = 0, height=1, \
                 width = 20)
R2.grid(row=3,column=2)
L5 = Label (text='Number of Loop').grid(row=4,column=0)
E5 = Entry(root, width=5)
E5.grid(row=4,column=1)
E5.config(state=DISABLED)
L2 = Label ().grid(row=21,column=1)
# List of Tests
bottomframe = Frame(root)
bottomframe.grid(row=24,column=1)
# Reset Button
configbutton = Button(bottomframe, text="Reset", fg="black")
configbutton.grid(row=24,column=2,padx=5, pady=0)
# Quit Button
configbutton = Button(bottomframe, text="Quit", fg="red")
configbutton.grid(row=24,column=3)
# Submit Button
blackbutton = Button(bottomframe, text="Submit", fg="black")
blackbutton.grid(row=24,column=4,padx=5, pady=0)

#Check buttons for test suite selection
test_suite_name=[name for name in os.listdir(".") if (os.path.isdir(name) and name.startswith('test-') )]
L34 = Label (text='Select Test Suite To Be Executed').grid(row=7,column=1)      
L11 = Label ().grid(row=9,column=1)
row_me =9
col_me =0
checkbuttons = [create_checkbutton(name) for name in test_suite_name ]
for c in checkbuttons:          
        c.config(state=DISABLED)
# initiate the loop
root.mainloop()

我已经编写了这段代码,但我不确定如何为每个小部件制作网格和位置pyqt

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        SubmitButton = QtGui.QPushButton("OK")
        CancelButton = QtGui.QPushButton("Cancel")
        ResetButton = QtGui.QPushButton("Reset")
        edit1 = QLineEdit()
        edit2 = QLineEdit()
        cb = QtGui.QCheckBox('Enable Test suite selection', self)
        cb.move(150, 100)
        #cb.stateChanged.connect(self.changeTitle)        
        cb2 = QtGui.QCheckBox('Send mail', self)
        cb2.move(300, 100)
       # cb2.stateChanged.connect(self.changeTitle)

        hbox = QtGui.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(SubmitButton)
        hbox.addWidget(CancelButton)
        hbox.addWidget(ResetButton)
        hbox.addWidget(edit1)
        hbox.addWidget(edit2)
        hbox.addWidget(cb)
        hbox.addWidget(cb2)

        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox)    

        self.setGeometry(100, 100, 500, 650)
        self.setWindowTitle('HMI')    
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

谢谢!

4

1 回答 1

1

您可以在代码中进行设计部分,也可以通过 PyQt4 附带的 QT Designer 进行。如果您使用的是 Windows,则可以在路径中的路径C:\Python26\Lib\site-packages\PyQt4\examples和文档中找到一些示例。C:\Python26\Lib\site-packages\PyQt4\doc\html\modules.html希望这可以帮助。

于 2013-04-27T08:02:15.307 回答