主文件有一个路径:С:\\myapp\app.py
工作还需要第二个文件:С:\\myapp\folder\additional_file.py
如何将文件additional_file.py
导入app.py
?
主文件有一个路径:С:\\myapp\app.py
工作还需要第二个文件:С:\\myapp\folder\additional_file.py
如何将文件additional_file.py
导入app.py
?
我尝试添加QVBoxLayout - QScrollArea
和QScrollBar
。
如layout
添加它们的小部件所示player
(2 件)。
滚动时,小部件成倍增加。如果它很简单,那么你需要做QScrollArea
and QScrollBar
,当滚动 which 时,它们会滚动player'ы
。
它应该看起来如何,大约(小部件移出):
我得到什么:
这是部分代码: Main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import sys, os
import player
class Main(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setFixedSize(750, 560)
self.p1 = player.player_1()
self.lay = QHBoxLayout(self)
self.lay.setContentsMargins(0,0,0,0)
self.scroll_area = QtWidgets.QScrollArea(self) # 2
self.scroll_area.setLayout(self.lay)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scrollbar = QtWidgets.QScrollBar(Qt.Horizontal, self) # 3
self.scrollbar.setMaximum(250)
self.scrollbar.valueChanged.connect(self.sync_func) # 4
self.v_layout = QtWidgets.QVBoxLayout(self)
self.v_layout.addWidget(self.scroll_area)
self.v_layout.addWidget(self.scrollbar)
def sync_func(self):
self.scroll_area.horizontalScrollBar().setValue(self.scrollbar.value() * 1)
self.lay.addWidget(Sttings())
self.lay.addWidget(player.player_1())
self.lay.addWidget(player.player_1())
self.transition()
def transition(self):
player.reputation_perem = player.reputation_perem + (player.reputation_perem / 100 * 5)
player.money_perem = player.money_perem + player.income_perem
#reputation_perem_2 = reputation_perem_2 + (reputation_perem_2 / 100 * 5)
#income_perem_2 = income_perem_old_2 / 100 * (100 - (100 - reputation_perem_2))
#money_perem_2 = money_perem_2 + income_perem_2
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
file = QtCore.QFile("C:/for cheacher/3_mate_files/dark.qss")
file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text)
stream = QtCore.QTextStream(file)
app.setStyleSheet(stream.readAll())
sys.exit(app.exec_())
播放器.py:
money_perem = 0
income_perem_old = 500
income_perem = 0
credit_perem = 0
reputation_perem = 20
class player_1(QtWidgets.QWidget):
def __init__(self, parent=None):
global money_perem, credit_perem, reputation_perem, income_perem, income_perem_old
super(player_1, self).__init__(parent)
self.setFixedSize(350, 550)
# изображение
self.Label_ing = QLabel('', self)
self.img = QtGui.QPixmap('C:/py/Monopolya/polzovatel_1.png')
self.Label_ing.setPixmap(self.img)
self.Label_ing.move(75, 20)
# манипуляции
# Валюта
self.PB_money = QPushButton('Валюта ₽', self)
self.PB_money.move(15, 260)
self.PB_money.resize(150, 25)
self.PB_money.setStyleSheet('font: 13pt \'Arial Black\'')
self.PB_money.clicked.connect(self.money_func)
# Репутация
self.PB_reputation = QPushButton('Репутация ⓇⓅ', self)
self.PB_reputation.move(185, 260)
self.PB_reputation.resize(150, 25)
self.PB_reputation.setStyleSheet('font: 13pt \'Arial Black\'')
self.PB_reputation.clicked.connect(self.reputation_func)
# Кредит
self.PB_credit = QPushButton("Кредит", self)
self.PB_credit.move(15, 300)
self.PB_credit.resize(150, 25)
self.PB_credit.setStyleSheet('font: 13pt \'Arial Black\'')
self.PB_credit.clicked.connect(self.credit_func)
# Доход
self.PB_income = QPushButton('Доход', self)
self.PB_income.move(185, 300)
self.PB_income.resize(150, 25)
self.PB_income.setStyleSheet('font: 13pt \'Arial Black\'')
self.PB_income.clicked.connect(self.income_func)
# информация
self.Label_Money = QLabel(self)
self.Label_Money.move(50, 350)
self.Label_Money.resize(400, 25)
self.Label_Money.setStyleSheet("font: 15pt \'Arial Black\'")
self.Label_reputation = QLabel(self)
self.Label_reputation.move(50, 400)
self.Label_reputation.resize(400, 25)
self.Label_reputation.setStyleSheet("font: 15pt \'Arial Black\'")
self.Label_income = QLabel(self)
self.Label_income.move(50, 450)
self.Label_income.resize(400, 25)
self.Label_income.setStyleSheet("font: 15pt \'Arial Black\'")
self.Label_credit = QLabel(self)
self.Label_credit.move(50, 500)
self.Label_credit.resize(400, 25)
self.Label_credit.setStyleSheet("font: 15pt \'Arial Black\'")
self.t = QtCore.QTimer(interval=100)
self.t.timeout.connect(self.update_info)
self.t.start()
def update_info(self):
global money_perem, reputation_perem, income_perem, credit_perem, income_perem_old
income_perem = income_perem_old / 100 * (100 - (100 - reputation_perem))
self.Label_Money.setText('Деньги: ' + str(money_perem / 1000000) + ' ' + "млн ₽")
self.Label_reputation.setText('Репутация: ' + str(reputation_perem) + ' ' + 'ⓇⓅ')
self.Label_income.setText('Доход: ' + str(income_perem / 1000) + ' ' + '(' + str(income_perem_old / 1000) + ')' + ' тыс ₽')
self.Label_credit.setText('Кредит: ' + str(credit_perem / 1000000) + ' ' + 'млн ₽')
def credit_func(self):
self.cr = credit()
self.cr.show()
def money_func(self):
self.m = money()
self.m.show()
def reputation_func(self):
self.rep = reputation()
self.rep.show()
def income_func(self):
self.incom = income()
self.incom.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
file = QtCore.QFile("C:/for cheacher/3_mate_files/dark.qss")
file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text)
stream = QtCore.QTextStream(file)
app.setStyleSheet(stream.readAll())
w = player_1()
w.show()
sys.exit(app.exec_())
关闭程序时如何询问是否关闭程序?
代码示例:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Main(object):
def __init__(self):
super(Main, self).__init__()
self.setFixedSize(423, 293)
self.lineEdit1 = QtWidgets.QLineEdit("", self)
self.lineEdit1.resize(321, 51)
self.lineEdit1.move(50, 80)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
有没有QLabel
办法改变文字颜色RGB
?
self.Label = QtWidgets.QLabel("", self)
self.Label.move(50, 30)
self.Label.resize(421, 41)
有窗户main
和 win
如果窗口打开,如果它关闭,则win
写入控制台。"окно открыто"
"окно закрыто"
怎么做?
代码示例:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main(object):
def setupUi(self, main):
main.setObjectName("main")
main.resize(400, 300)
self.label = QtWidgets.QLabel(main)
self.label.setGeometry(QtCore.QRect(20, 20, 111, 41))
self.label.setStyleSheet("font: 87 16pt \"Arial Black\";")
self.label.setObjectName("label")
self.pushButton_1 = QtWidgets.QPushButton(main)
self.pushButton_1.setGeometry(QtCore.QRect(10, 10, 50, 31))
self.pushButton_1.setStyleSheet("font: 87 14pt \"Arial Black\";")
self.pushButton_1.setObjectName("pushButton")
self.retranslateUi(main)
QtCore.QMetaObject.connectSlotsByName(main)
def retranslateUi(self, main):
_translate = QtCore.QCoreApplication.translate
main.setWindowTitle(_translate("main", "Form"))
self.label.setText(_translate("main", "main"))
self.pushButton_1.setText(_translate("main", "нажать"))
class Ui_window_1(object):
def setupUi(self, window_1):
window_1.setObjectName("window_1")
window_1.resize(400, 300)
self.label = QtWidgets.QLabel(window_1)
self.label.setGeometry(QtCore.QRect(20, 20, 111, 41))
self.label.setStyleSheet("font: 87 16pt \"Arial Black\";")
self.label.setObjectName("label")
self.retranslateUi(window_1)
QtCore.QMetaObject.connectSlotsByName(window_1)
def retranslateUi(self, window_1):
_translate = QtCore.QCoreApplication.translate
window_1.setWindowTitle(_translate("window_1", "Form"))
self.label.setText(_translate("window_1", "window_1"))
class Win(QtWidgets.QWidget, Ui_window_1):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
class Main(QtWidgets.QWidget, Ui_main):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
b = #определение открыто ли окно или нет
if b == True:
print("Окно открыто")
else:
print("Окно закрыто")
self.setupUi(self)
self.pushButton_1.clicked.connect(self.Win_f)
def Win_f(self):
self.wmain = Win()
self.wmain.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
再次调用 och 函数时,报错:
self.och()
TypeError: 'Myoch' object is not callable
这是代码的一部分:
def opr(self):
path = File_location + "1_classes/" + self.lineEdit1.text() + '.txt'
try:
file = open(path)
except IOError as e:
self.och()
else:
self.uch()
och函数:
def och(self):
self.och = Myoch()
self.och.show()
我有一个程序:
import time
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1110, 671)
self.la_non = QtWidgets.QLabel(Form)
self.la_non.setGeometry(QtCore.QRect(10, 10, 1091, 71))
self.la_non.setStyleSheet("font: 48pt \"Molot\";")
self.la_non.setObjectName("la_non")
self.label_lime = QtWidgets.QLabel(Form)
self.label_lime.setGeometry(QtCore.QRect(20, 150, 1071, 491))
self.label_lime.setStyleSheet("font: 55pt \"Molot\";")
self.label_lime.setObjectName("label_lime")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.la_non.setText(_translate("Form", "1234"))
class Main(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
def time(self):
import time
time = str(time.strftime("%X"))
hour, minute, second = time.split(':')
self.label_lime.setText(str(23 - int(hour)) + " " + "часов" + " " + str(59 - int(minute)) + " " +"минут" + " " + str(60 - int(second)) + " " + "секунд")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
你需要循环函数时间。
如何确定文本超出范围的字符label
?
从15
字符开始,文本走出了窗口。
如何移动一条线?
例子:
а б в г д е е е д д д д д о о о о о к к к к
1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 и т.д
如何在调整窗口大小时更改其中的小部件的大小。
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1000, 834)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(200, 510, 541, 121))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(230, 100, 481, 201))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "PushButton"))
class Main(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
while True:
width = "определение ширины"
height = "определение высоты"
x = width - 1000
y = height - 834
Form.resize(1000 + x, 834 + y)
self.pushButton.setGeometry(541 + x, 121 + y)
self.lineEdit.setGeometry(481 + x, 201 + y)
from selenium import webdriver
def main():
driver = webdriver.Chrome()
url = "https://vk.com/"
driver.get(url)
login = driver.find_element_by_name_name("email")
login.clear()
login.send_keys("123")
pswd = driver.find_element_by_name_name('pass')
pswd.send_keys("123")
but_log = driver.find_element_by_class_name("index_login_button")
but_log.click()
main()
代码抛出错误
$ C:\py\piramida.py
Traceback (most recent call last):
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] Не удается найти указанный файл
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\py\piramida.py", line 13, in <module>
main()
File "C:\py\piramida.py", line 3, in main
driver = webdriver.Chrome()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\Danila\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
如何确定CheckBox
激活了什么?
比方说,如果它checkBox
处于活动状态,则显示 1,如果它未处于活动状态,则显示 2。
怎么做?如果小部件上有“勾号”,我认为是活动的。
с = #определение состояния
if c == #есть "галочка":
x = 5
elif c == #нет "галочки":
x = 10
print(x)
#что-то не так
import subprocess
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(959, 365)
Form.setStyleSheet("")
self.checkBox = QtWidgets.QCheckBox(Form)
self.checkBox.setGeometry(QtCore.QRect(20, 230, 421, 41))
self.checkBox.setObjectName("checkBox")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(23, 30, 441, 41))
self.label.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(494, 20, 421, 61))
self.label_2.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 80, 421, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 80, 431, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(25, 164, 421, 21))
self.label_3.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(493, 160, 421, 31))
self.label_4.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_4.setObjectName("label_4")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(21, 190, 421, 41))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(489, 190, 431, 41))
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(20, 280, 901, 51))
self.pushButton.setStyleSheet("font: 87 26pt \"Arial Black\";")
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.checkBox.stateChanged.connect(
lambda state=self.checkBox.isChecked(), no=1: self.selectBooks(state, no))
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox.setText(_translate("Form", "Использовать тот же путь"))
self.label.setText(_translate("Form", "Введи путь до исходного файла"))
self.label_2.setText(_translate("Form", "Введи название исходного файла"))
self.label_3.setText(_translate("Form", "Введи путь до конечного файла"))
self.label_4.setText(_translate("Form", "Введи название конечного файла"))
self.pushButton.setText(_translate("Form", "Конвертировать"))
app = QApplication([])
class Main(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
file = str(self.lineEdit.text()) + "/" + str(self.lineEdit_2)
def selectBooks(self, toggle, no):
if toggle == QtCore.Qt.Checked:
print('toggle=`{}`, checked_{}'.format(toggle, no))
if no == 1:
print('checked_{} -> галочка поставлена выполнилось действие'.format(no))
else:
print('toggle=`{}`, unchecked_{}'.format(toggle, no))
mw = Main()
mw.show()
app.exec()
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(959, 365)
Form.setStyleSheet("")
self.checkBox = QtWidgets.QCheckBox(Form)
self.checkBox.setGeometry(QtCore.QRect(20, 230, 421, 41))
self.checkBox.setObjectName("checkBox")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(23, 30, 441, 41))
self.label.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(494, 20, 421, 61))
self.label_2.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(20, 80, 421, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Form)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 80, 431, 41))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(25, 164, 421, 21))
self.label_3.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(493, 160, 421, 31))
self.label_4.setStyleSheet("font: 87 12pt \"Arial Black\";")
self.label_4.setObjectName("label_4")
self.lineEdit_3 = QtWidgets.QLineEdit(Form)
self.lineEdit_3.setGeometry(QtCore.QRect(21, 190, 421, 41))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(Form)
self.lineEdit_4.setGeometry(QtCore.QRect(489, 190, 431, 41))
self.lineEdit_4.setObjectName("lineEdit_4")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(20, 280, 901, 51))
self.pushButton.setStyleSheet("font: 87 26pt \"Arial Black\";")
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox.setText(_translate("Form", "Использовать тот же путь"))
self.label.setText(_translate("Form", "Введи путь до исходного файла"))
self.label_2.setText(_translate("Form", "Введи название исходного файла"))
self.label_3.setText(_translate("Form", "Введи путь до конечного файла"))
self.label_4.setText(_translate("Form", "Введи название конечного файла"))
self.pushButton.setText(_translate("Form", "Конвертировать"))
class Main(QtWidgets.QWidget, Ui_Form):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
file = self.lineEdit.Text() + "/" + self.lineEdit_2
x = self.checkBox.isChecked()
界面应该打开了,但是打不开,怎么办?
我尝试了两种方法:
一。
f = path(file_location + '1_classes/' + self.lineEdit1.text() + '.txt')
if f.is_file():
self.uch()
file.close()
else:
self.och()
file.close()
2.
try:
file = open(file_location + "1_classes/" + self.lineEdit1.text() + '.txt')
except IOError as e:
self.och()
file.close()
else:
self.uch()
file.close()
两种方式都行不通。如有必要,我找到了文件的路径,如下所示:
file_location + '1_classes/' + self.lineEdit1.text() + '.txt'
路径形成正确,检查。
即使文件存在,代码也会打开一个报告错误的窗口。
我有一个程序,其中有三个窗口。我不明白如何只使用一个窗口(显示文本,使用按钮)。
这是代码:main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from ui_inst import Ui_Inst
from ui_uch import Ui_Uch
class Ui_Start(object):
def setupUi(self, Ui_Start):
Ui_Start.setObjectName("Ui_Start")
Ui_Start.resize(443, 293)
self.lineEdit1 = QtWidgets.QLineEdit(Ui_Start)
self.lineEdit1.setGeometry(QtCore.QRect(50, 80, 321, 51))
self.lineEdit1.setStyleSheet("")
self.lineEdit1.setText("")
self.lineEdit1.setObjectName("lineEdit")
self.textEdit = QtWidgets.QTextEdit(Ui_Start)
self.textEdit.setGeometry(QtCore.QRect(10, 30, 421, 41))
self.textEdit.setObjectName("textEdit")
self.pushButton_2 = QtWidgets.QPushButton(Ui_Start)
self.pushButton_2.setGeometry(QtCore.QRect(150, 210, 141, 58))
self.pushButton_2.setStyleSheet("font: 16pt \"Molot\";")
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton = QtWidgets.QPushButton(Ui_Start)
self.pushButton.setGeometry(QtCore.QRect(50, 140, 321, 58))
self.pushButton.setStyleSheet("font: 20pt \"Molot\";")
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Ui_Start)
QtCore.QMetaObject.connectSlotsByName(Ui_Start)
def retranslateUi(self, Ui_Start):
_translate = QtCore.QCoreApplication.translate
Ui_Start.setWindowTitle(_translate("Ui_Start", "Ui_Start"))
self.textEdit.setHtml(_translate("Ui_Start", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:16pt;\">Введите номер и букву класса:</span></p></body></html>"))
self.pushButton_2.setText(_translate("Ui_Start", "инструкция"))
self.pushButton.setText(_translate("Ui_Start", "Найти класс"))
class MyInst(QtWidgets.QWidget, Ui_Inst):
def __init__(self, parent=None):
super(MyInst, self).__init__(parent)
self.setupUi(self)
class MyUch(QtWidgets.QWidget, Ui_Uch):
def __init__(self, parent=None):
super(MyUch, self).__init__(parent)
self.setupUi(self)
class Main(QtWidgets.QWidget, Ui_Start):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setupUi(self)
self.pushButton_2.clicked.connect(self.onClicked)
self.pushButton.clicked.connect(self.uch)
def onClicked(self):
self.inst = MyInst()
self.inst.show()
def uch(self):
self.uch = MyUch()
self.uch.show()
self.lineEdit2.setText("1")
StyleSheet = '''
QPushButton {
font: bold italic 16pt 'Comic Sans MS';
background-color: silver;
width: 75px ;
height: 50px;
border: none;
text-align: center;
}
QPushButton:hover {
background: #C9C0BB;
}
QPushButton:pressed {
background-color: blue;
}
'''
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(StyleSheet)
file = QtCore.QFile("dark.qss")
file.open(QtCore.QFile.ReadOnly | QtCore.QFile.Text)
stream = QtCore.QTextStream(file)
app.setStyleSheet(stream.readAll())
w = Main()
w.show()
sys.exit(app.exec_())
ui_inst.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Inst(object):
def setupUi(self, Inst):
Inst.setObjectName("Ui_Inst")
Inst.resize(1111, 883)
self.textEdit = QtWidgets.QTextEdit(Inst)
self.textEdit.setGeometry(QtCore.QRect(0, 10, 1111, 41))
self.textEdit.setObjectName("textEdit")
self.text_inctru = QtWidgets.QTextEdit(Inst)
self.text_inctru.setGeometry(QtCore.QRect(20, 60, 1071, 811))
self.text_inctru.setObjectName("text_inctru")
self.retranslateUi(Inst)
QtCore.QMetaObject.connectSlotsByName(Inst)
def retranslateUi(self, Inst):
_translate = QtCore.QCoreApplication.translate
Inst.setWindowTitle(_translate("Inst", "Inst"))
self.textEdit.setHtml(_translate("Inst", "<!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-
html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style
type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-
size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px;
margin-left:0px; margin-right:0px; -qt-block-indent:0; text-
indent:0px;\"><span style=\" font-size:22pt;\">Инструкция</span></p>
</body></html>"))
ui_uch.py :
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Uch(object):
def setupUi(self, Uch):
Uch.setObjectName("Uch")
Uch.resize(718, 336)
self.lineEdit = QtWidgets.QLineEdit(Uch)
self.lineEdit.setGeometry(QtCore.QRect(60, 40, 611, 81))
self.lineEdit.setStyleSheet("font: 20pt \"Molot\";")
self.lineEdit.setObjectName("lineEdit1")
self.pushButton = QtWidgets.QPushButton(Uch)
self.pushButton.setGeometry(QtCore.QRect(190, 140, 341, 51))
self.pushButton.setStyleSheet("font: 27pt \"Molot\";")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Uch)
self.pushButton_2.setGeometry(QtCore.QRect(60, 222, 601, 61))
self.pushButton_2.setStyleSheet("font: 45pt \"Molot\";")
self.pushButton_2.setObjectName("pushButton_2")
self.label = QtWidgets.QLabel(Uch)
self.label.setGeometry(QtCore.QRect(70, 0, 601, 41))
self.label.setStyleSheet("font: 25pt \"Molot\";")
self.label.setObjectName("label")
self.retranslateUi(Uch)
QtCore.QMetaObject.connectSlotsByName(Uch)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Uch", "Uch"))
self.pushButton.setText(_translate("Uch", "Выбрать ученика"))
self.pushButton_2.setText(_translate("Uch", "Задать вопрос"))
self.label.setText(_translate("Uch", "Отвечает ученик:"))
我有两个窗户。
如何,当您单击窗口中的“指令”按钮时start
,打开窗口inst
?
开始(开始窗口):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Start(object):
def setupUi(self, Start):
Start.setObjectName("Ui_Start")
Start.resize(443, 293)
Start.setStyleSheet("QPushButton:hover {backgroun-color: red} \n"
"QPushButton:pressed {backgroun-color: blue\n"
"}\n"
"QPushButton{\n"
" font: 45pt \"Molot\";\n"
" backgroun-color: white;\n"
" width: 75px ;\n"
" height: 50px;\n"
" font-size: 45px;\n"
" font-weight: bold;\n"
" bolder: none;\n"
" text-align: center\n"
"}\n"
"\n"
"")
self.lineEdit = QtWidgets.QLineEdit(Start)
self.lineEdit.setGeometry(QtCore.QRect(50, 80, 321, 51))
self.lineEdit.setStyleSheet("")
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.textEdit = QtWidgets.QTextEdit(Start)
self.textEdit.setGeometry(QtCore.QRect(10, 30, 421, 41))
self.textEdit.setObjectName("textEdit")
self.pushButton_2 = QtWidgets.QPushButton(Start)
self.pushButton_2.setGeometry(QtCore.QRect(150, 210, 141, 58))
self.pushButton_2.setStyleSheet("font: 16pt \"Molot\";")
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton = QtWidgets.QPushButton(Start)
self.pushButton.setGeometry(QtCore.QRect(50, 140, 321, 58))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Start)
QtCore.QMetaObject.connectSlotsByName(Start)
def retranslateUi(self, v):
_translate = QtCore.QCoreApplication.translate
Start.setWindowTitle(_translate("Start", "Start"))
self.textEdit.setHtml(_translate("Start", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:16pt;\">введите номер и букву класса класса :</span></p></body></html>"))
self.pushButton_2.setText(_translate("Start", "инструкция"))
self.pushButton.setText(_translate("Start", "1"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Start = QtWidgets.QWidget()
ui = Ui_Start()
ui.setupUi(Start)
Start.show()
sys.exit(app.exec_())
inst(指令):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Inst(object):
def setupUi(self, Inst):
Inst.setObjectName("Ui_Inst")
Inst.resize(1111, 883)
self.textEdit = QtWidgets.QTextEdit(v)
self.textEdit.setGeometry(QtCore.QRect(0, 10, 1111, 41))
self.textEdit.setObjectName("textEdit")
self.text_inctru = QtWidgets.QTextEdit(Inst)
self.text_inctru.setGeometry(QtCore.QRect(20, 60, 1071, 811))
self.text_inctru.setObjectName("text_inctru")
self.retranslateUi(Inst)
QtCore.QMetaObject.connectSlotsByName(v)
def retranslateUi(self, Inst):
_translate = QtCore.QCoreApplication.translate
Inst.setWindowTitle(_translate("Inst", "Inst"))
self.textEdit.setHtml(_translate("Inst", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:22pt;\">Инструкция</span></p></body></html>"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Inst = QtWidgets.QWidget()
ui = Ui_Inst()
ui.setupUi(Inst)
Inst.show()
sys.exit(app.exec_())
如何打开第二个窗口?
from PySide import QtCore, QtGui
import sys
from ui import Ui_Start
import time
app = QtGui.QApplication(sys.argv)
Start = QtGui.QWidget()
ui = Ui_Start()
ui.setupUi(Start)
Start.show()
def but_1():
from ui import Ui_inst
app = QtGui.QApplication(sys.argv)
inst = QtGui.QWidget()
ui_ins = Ui_inst()
ui_ins.setupUi(inst)
inst.show()
ui_ins.textEdit.setText("12546")
sys.exit(app.exec_())
ui.pushButton_2.clicked.connect( but_1 )
sys.exit(app.exec_())
有一个代码:
from PySide import QtCore, QtGui
import sys
from ui import Ui_Form
import time
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
t = ""
# функции
def but1():
ui.lineEdit.setText(ui.lineEdit.text() + "1")
def but2():
ui.lineEdit.setText(ui.lineEdit.text() + "2")
def but3():
ui.lineEdit.setText(ui.lineEdit.text() + "3")
def but4():
ui.lineEdit.setText(ui.lineEdit.text() + "4")
def but5():
ui.lineEdit.setText(ui.lineEdit.text() + "5")
def but6():
ui.lineEdit.setText(ui.lineEdit.text() + "6")
def but7():
ui.lineEdit.setText(ui.lineEdit.text() + "7")
def but8():
ui.lineEdit.setText(ui.lineEdit.text() + "8")
def but9():
ui.lineEdit.setText(ui.lineEdit.text() + "9")
def but_pl():
ui.lineEdit.text() t
ui.lineEdit.setText("")
time.sleep(0.4)
ui.lineEdit.setText(t)
# кнопки
ui.but1.clicked.connect( but1 )
ui.but2.clicked.connect( but2 )
ui.but3.clicked.connect( but3 )
ui.but4.clicked.connect( but4 )
ui.but5.clicked.connect( but5 )
ui.but6.clicked.connect( but6 )
ui.but7.clicked.connect( but7 )
ui.but8.clicked.connect( but8 )
ui.but9.clicked.connect( but9 )
ui.but_pl.clicked.connect( but_pl )
# основной код
sys.exit(app.exec_())
现在让我们关注函数def but_pl()
。
需要将\u200b\u200bof的值lineEdit
写入变量,然后lineEdit
清零。在这种情况下,我将变量中的值输出回lineEdit
.
告诉我如何实现它?