我有一个程序,该程序分几个步骤进行了编程(每个页面本身),现在在组装它们时遇到了问题。 各个Windows都为自己工作,但是Mainwindow的工作程序一起无法正常工作。由于我对PyQt几乎一无所知,因此错误可能也很明显。
更新:现在程序可以工作了,但是图片仅在按下按钮时才开始加载(在我删除其他线程的那一刻)。现在我的问题是如何自动执行此操作。
import sys
from PyQt5 import QtCore, QtWidgets, QtGui
import time
import os
from PyQt5.QtGui import QIcon
path = "C:/Users/benni/Desktop/"
folder_name = "exit_folder_test"
name_list = "used_names.txt"
source = "test"
extension = "jpg"
if not os.path.isfile(os.path.join(path, name_list)):
used_names = open(path + name_list, "w+")
used_names.close()
names =[]
else:
used_names = open(path + name_list, "r")
names = used_names.readlines()
used_names.close()
print(names)
#####################################################################################
class MainWindow(QtWidgets.QMainWindow):
switch_window = QtCore.pyqtSignal()
def __init__(self):
super(MainWindow, self).__init__()
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
grid_layout = QtWidgets.QGridLayout(widget)
rows, cols = 2, 3
self._labels = []
global image_width, image_height, number, pictures
image_width, image_height, number = 600, 400, 0
pictures = []
for row in range(rows):
for col in range(cols):
label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
label.setFixedSize(image_width, image_height)
grid_layout.addWidget(label, row, col)
self._labels.append(label)
self.ComboBox = QtWidgets.QComboBox(self)
grid_layout.addWidget(self.ComboBox, 3, 2, alignment=QtCore.Qt.AlignRight)
self.ComboBox.setFixedSize(125,25)
self.refresh_button = QtWidgets.QPushButton("update") # update button (this one I want to remove)
grid_layout.addWidget(self.refresh_button, 3, 1)
self.refresh_button.setFixedSize(125,25)
self.refresh_button.clicked[bool].connect(self.update)
self.print_button = QtWidgets.QPushButton("print") # print button
grid_layout.addWidget(self.print_button, 3, 3)
self.print_button.setFixedSize(75,25)
self.print_button.clicked[bool].connect(self.print)
self.resize(1920, 1080)
def update(self):
global number, pictures
for file in os.listdir(os.path.join(path, source)): # add all files with ".jpg" to the list pictures
if file.endswith(".{}".format(extension)):
pictures.append(file)
if len(pictures) > 0:
for picture_name in pictures:
number += 1
if 0 < number < 7:
time.sleep(1) # wait until the picture is transferred
picture_new_name = "image{}.{}".format(number, extension)
os.rename(
os.path.join(path, source, picture_name),
os.path.join(path, folder_name, picture_new_name),
) # renames all the given pictures and transfer them in a new folder
print(number)
time.sleep(0.2)
label = self._labels[number-1]
pixmap = QtGui.QPixmap(os.path.join(path, folder_name, "image{}.{}".format(number, extension))).scaled(image_width, image_height)
label.setPixmap(pixmap)
self.ComboBox.addItem("image{}.{}".format(number, extension))
del pictures[:]
def print(self):
print("test")
self.switch_window.emit()
###########################################################################################################
class WindowTwo(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Window Two')
layout = QtWidgets.QGridLayout()
self.finish_button = QtWidgets.QPushButton('finish')
self.finish_button.clicked.connect(self.finished)
self.finish_button.setFixedSize(75,23)
layout.addWidget(self.finish_button, 3,3)
self.setLayout(layout)
self.resize(1920, 1080)
def finished(self):
self.switch_window.emit() # reset
###########################################################################################################
class Login(QtWidgets.QWidget):
switch_window = QtCore.pyqtSignal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setWindowTitle('Fotobox')
self.resize(1920,1080)
layout = QtWidgets.QGridLayout()
self.Button = QtWidgets.QPushButton('next')
self.Button.setFixedSize(85,30)
layout.addWidget(self.Button, 3, 3)
self.Button.clicked.connect(self.on_click)
self.label = QtWidgets.QLabel("name: ")
self.label.setFixedSize(120,51)
layout.addWidget(self.label, 1, 1)
self.error_label = QtWidgets.QLabel("")
self.error_label.setFixedSize(120,51)
layout.addWidget(self.error_label, 2,2)
self.lineEdit = QtWidgets.QLineEdit()
self.lineEdit.setFixedSize(260,31)
layout.addWidget(self.lineEdit, 1, 2)
self.lineEdit.textChanged.connect(self.issue_test)
self.setLayout(layout)
def issue_test(self):
self.issue = False
name = self.lineEdit.text()
for i in names:
if name == i or name+"\n" == i:
self.error_label.setText("error")
self.issue = True
if self.issue == False:
self.issue = False
self.error_label.setText("")
def on_click(self):
global folder_name
name = self.lineEdit.text()
if name == "":
self.error_label.setText("error")
self.issue = True
if self.issue == False:
folder_name = name
os.mkdir(path + folder_name)
names.append(folder_name)
used_names = open(path + name_list, "a")
used_names.write(name + "\n")
used_names.close()
self.switch_window.emit() # switch to the mainwindow
######################################################################################################################################################
class Controller:
def __init__(self):
pass
def show_login(self):
self.login = Login()
try: self.window_two.close()
except:
print("first time")
self.login.switch_window.connect(self.show_main)
self.login.show()
def show_main(self):
self.window = MainWindow()
self.window.switch_window.connect(self.show_window_two)
self.login.close()
self.window.show()
def show_window_two(self):
self.window_two = WindowTwo()
self.window.close()
self.window_two.switch_window.connect(self.show_login)
self.window_two.show()
def main():
app = QtWidgets.QApplication(sys.argv)
controller = Controller()
controller.show_login()
sys.exit(app.exec_())
if __name__ == '__main__':
main()