Уведомления

Группа в Telegram: @pythonsu

#1 Июль 17, 2019 21:35:47

py.user.next
От:
Зарегистрирован: 2010-04-29
Сообщения: 8683
Репутация: +  770  -
Профиль   Отправить e-mail  

Python PyQt5 QLabel Обновление данных

Тут заменил базу на заглушку и исправил несколько ошибок

  
#!/usr/bin/env python3
 
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
 
import time
 
from PyQt5 import QtCore
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QTableView
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
 
#import pymysql.cursors
#import pymysql
#pymysql.install_as_MySQLdb()
 
#import MySQLdb
 
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QDialog, QDialogButtonBox,
        QHBoxLayout, QMessageBox, QPushButton, QTableView)
 
from PyQt5.QtSql import QSqlTableModel
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QTableView
 
from PyQt5.QtSql import QSqlTableModel
 
 
class Execute:
 
    def execute(self, *args, **kwargs):
        return 1
 
    def fetchall(self, *args, **kwargs):
        return []
 
class Cursor:
 
    def cursor(self):
        execute = Execute()
        return execute
 
class MySQLdb:
 
    def connect(self, *args, **kwargs):
        cursor = Cursor()
        return cursor
 
 
try:
    _fromUtf8 = QtCore.QString.fromUtf8
 
except AttributeError:
    def _fromUtf8(s):
        return s
 
class Widget(QtWidgets.QWidget):
 
    def __init__(self):
        super().__init__()
        self.starw()
 
    def starw(self):
        self.window_height = 300
        self.window_width = 600
        self.setFixedSize(self.window_height, self.window_width)
        self.setWindowIcon(QIcon('logo1.png'))
        self.setWindowTitle("Teco monitoring")
        layout = QtWidgets.QVBoxLayout(self)
        btn_layout = QtWidgets.QHBoxLayout()
        btn1 = QtWidgets.QPushButton("Previous")
        btn2 = QtWidgets.QPushButton("Quit")
        btn3 = QtWidgets.QPushButton("Next")
        btn_layout.addWidget(btn1)
        btn_layout.addWidget(btn2)
        btn_layout.addWidget(btn3)
        btn1.clicked.connect(self.minus)
        btn2.clicked.connect(QCoreApplication.instance().quit)
        btn3.clicked.connect(self.plus)
        self.table = table = QtWidgets.QTableWidget()
        table.setRowCount(6)
        table.setColumnCount(2)
        table.setHorizontalHeaderLabels(["Mark", "Devise"])
        table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        layout.addWidget(table)
        layout.addLayout(btn_layout)
        self.baza(table)
 
    def baza(self, table):
        self.kolstrok = 1
        self.kolnew = 1
        db = MySQLdb.connect("", "", "", "")
        print("connect DB successful!!")
        cursor = db.cursor()
        cursor.execute("select * from _data_in_memory  WHERE device_id=" + str(self.kolstrok))
        for dan in cursor.fetchall():
            print(self.kolstrok)
            table.setItem(0, 0, QTableWidgetItem("IP"))
            table.setItem(0, 1, QTableWidgetItem(str(dan[0])))
            table.setItem(1, 0, QTableWidgetItem("Titul"))
            table.setItem(1, 1, QTableWidgetItem(str(self.kolstrok)))
            table.setItem(2, 0, QTableWidgetItem("Model"))
            table.setItem(2, 1, QTableWidgetItem(dan[2]))
            table.setItem(3, 0, QTableWidgetItem("Mame"))
            table.setItem(3, 1, QTableWidgetItem(dan[3]))
            table.setItem(4, 0, QTableWidgetItem("Regoin"))
            table.setItem(4, 1, QTableWidgetItem(dan[4]))
            table.setItem(5, 0, QTableWidgetItem("Adres"))
            table.setItem(5, 1, QTableWidgetItem(dan[5]))
    
    def plus(self):
        self.kolstrok = self.kolstrok + 1
        if self.kolstrok > self.kolnew:
            self.kolstrok = 1
        else:
            self.kolstrok = 2
        print(self.kolstrok)
        self.baza(self.table)
 
    def minus(self):
        self.kolstrok = self.kolstrok - 1
        if self.kolstrok == 0:
            self.kolstrok = 1
        print(self.kolstrok)
        self.baza(self.table)
        
if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    w = Widget()
    w.show()
    app.exec()

Всё запускается, кнопки работают.


Здесь процесс исправления ошибок
[guest@localhost bd]$ ./t.py 
QSettings::value: Empty key passed
QSettings::value: Empty key passed
connect DB successful!!
0
Traceback (most recent call last):
File "./t.py", line 130, in minus
self.baza()
TypeError: baza() missing 1 required positional argument: 'table'
[guest@localhost bd]$ ./t.py
QSettings::value: Empty key passed
QSettings::value: Empty key passed
connect DB successful!!
0
Traceback (most recent call last):
File "./t.py", line 116, in plus
self.kolstrok = str(self.kolstrok + 1)
TypeError: must be str, not int
Aborted (стек памяти сброшен на диск)
[guest@localhost bd]$ ./t.py
QSettings::value: Empty key passed
QSettings::value: Empty key passed
connect DB successful!!
0
connect DB successful!!
0
connect DB successful!!
Traceback (most recent call last):
File "./t.py", line 117, in plus
if (self.kolstrok > self.kolnew):
AttributeError: 'Widget' object has no attribute 'kolnew'
[guest@localhost bd]$ ./t.py
QSettings::value: Empty key passed
QSettings::value: Empty key passed
connect DB successful!!
0
connect DB successful!!
0
connect DB successful!!
Traceback (most recent call last):
File "./t.py", line 118, in plus
if (self.kolstrok > self.kolnew):
TypeError: '>' not supported between instances of 'str' and 'int'
[guest@localhost bd]$ ./t.py
QSettings::value: Empty key passed
QSettings::value: Empty key passed
connect DB successful!!
0
connect DB successful!!
0
connect DB successful!!
1
connect DB successful!!
1
connect DB successful!!
[guest@localhost bd]$

Говорил же: запусти программу из консоли. Сам бы все ошибки увидел и исправил.



Отредактировано py.user.next (Июль 17, 2019 21:45:02)

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version