我想在PyQt中制作一个简单的GUI-App。单击按钮后,应该带您进入下一页。就像您在安装程序时知道的这些安装对话框一样。
我试图在与MainWindow完全相同的位置打开一个新窗口,但是感觉不正确。
S,这是我的代码:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "MainWindow"
# Here are some height & width variables
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.UiComponents()
def UiComponents(self):
self.searchButton = QPushButton("", self)
# alot of UiComponents go here
self.searchButton.clicked.connect(self.make_handleButton("searchButton"))
def make_handleButton(self, button):
def handleButton():
if button == "searchButton":
### here it should go to SearchWindow ###
#elif button == "importButton":
# self.importWindow()
return handleButton
class SearchWindow(QMainWindow):
def __init__(self):
super().__init__()
self.title = "Search for something"
# Here are some height & width variables
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.UiComponents()
self.hide()
def goToMain(self):
### here it should go back to the MainWindow ###
def UiComponents(self):
self.backButton = QPushButton("BackButton", self)
self.backButton.setGeometry(QRect(5, 5, self.backButtonWidth, self.backButtonHeight))
self.backButton.clicked.connect(self.goToMain)
答案 0 :(得分:2)
您必须使用QStackWidget,在下面的部分中有一个示例:
from PyQt5 import QtCore, QtGui, QtWidgets
class PageWindow(QtWidgets.QMainWindow):
gotoSignal = QtCore.pyqtSignal(str)
def goto(self, name):
self.gotoSignal.emit(name)
class MainWindow(PageWindow):
def __init__(self):
super().__init__()
self.initUI()
self.setWindowTitle("MainWindow")
def initUI(self):
self.UiComponents()
def UiComponents(self):
self.searchButton = QtWidgets.QPushButton("", self)
self.searchButton.clicked.connect(
self.make_handleButton("searchButton")
)
def make_handleButton(self, button):
def handleButton():
if button == "searchButton":
self.goto("search")
return handleButton
class SearchWindow(PageWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("Search for something")
self.UiComponents()
def goToMain(self):
self.goto("main")
def UiComponents(self):
self.backButton = QtWidgets.QPushButton("BackButton", self)
self.backButton.setGeometry(QtCore.QRect(5, 5, 100, 20))
self.backButton.clicked.connect(self.goToMain)
class Window(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.stacked_widget = QtWidgets.QStackedWidget()
self.setCentralWidget(self.stacked_widget)
self.m_pages = {}
self.register(MainWindow(), "main")
self.register(SearchWindow(), "search")
self.goto("main")
def register(self, widget, name):
self.m_pages[name] = widget
self.stacked_widget.addWidget(widget)
if isinstance(widget, PageWindow):
widget.gotoSignal.connect(self.goto)
@QtCore.pyqtSlot(str)
def goto(self, name):
if name in self.m_pages:
widget = self.m_pages[name]
self.stacked_widget.setCurrentWidget(widget)
self.setWindowTitle(widget.windowTitle())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
答案 1 :(得分:0)
我建议您不要使用pyqt5这么复杂,也不要因此而讨厌编程。用python okey写代码?并在python中使用argparse,这样我们就可以在创建Visual Studio之后从终端调用它,并打开c#项目设计自己的ui,然后使用args多数民众赞成在调用.py文件。
timer1.Enabled = true;
progressBar1.Value += 10;
string myPythonApp = "C://Users//giris//Desktop//staj_proje_son//main.py";
string cmdArguments = "/c \"python " + myPythonApp + " " + "--ogrencioptik " + textBox2.Text + " " + "--cevapkagidi " + textBox1.Text + " " + "--sonuckayit " + textBox3.Text + "\"";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "cmd.exe";
start.UseShellExecute = false;
start.WorkingDirectory = "C://Users//giris//Desktop//staj_proje_son//";
start.Arguments = cmdArguments;
start.RedirectStandardOutput = false;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
Process process = Process.Start(start);
timer1.Start();
process.WaitForExit();
timer1.Stop();
timer1.Enabled = false;
progressBar1.Value = 100;
MessageBox.Show("İşlem Bitti");
button3.Enabled = true;
这是我的c#调用python处理代码。试试这个,喜欢编程。想想简单。祝你有美好的一天。