在下面的代码中 self.tableWidget.resize(self.width()-20,self.height()-20)
self.tableWidget.move(10,20)
这两行没有起作用?不知为何?
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pandas as pd
import numpy as np
class Ui_MainWindow(QMainWindow):
def __init__(self):
super(QtWidgets.QMainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(666, 488)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.retranslateUi(MainWindow)
self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 40, self.geometry().width(), self.geometry().height()-40))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.tableWidget.setStyleSheet("selection-background-color:pink")
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.raise_()
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(90, 20, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("打开")
MainWindow.setCentralWidget(self.centralWidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.clicked.connect(self.openfile)
self.pushButton.clicked.connect(self.creat_table_show)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "一颗数据小白菜"))
def openfile(self):
###获取路径===================================================================
openfile_name = QFileDialog.getOpenFileName(self,'选择文件','','Excel files(*.xlsx , *.xls)')
#print(openfile_name)
global path_openfile_name
###获取路径====================================================================
path_openfile_name = openfile_name[0]
def resizeEvent(self, e):
#self.tableWidget.setGeometry(QtCore.QRect(0, 40, self.geometry().width(), self.geometry().height()-40))
#QtWidgets.QWidget.resizeEvent(self, e)
self.tableWidget.resize(self.width()-20,self.height()-20)
self.tableWidget.move(10,20)
def creat_table_show(self):
###===========读取表格,转换表格,===========================================
if len(path_openfile_name) > 0:
input_table = pd.read_excel(path_openfile_name)
#print(input_table)
input_table_rows = input_table.shape[0]
input_table_colunms = input_table.shape[1]
#print(input_table_rows)
#print(input_table_colunms)
input_table_header = input_table.columns.values.tolist()
#print(input_table_header)
###===========读取表格,转换表格,============================================
###======================给tablewidget设置行列表头============================
self.tableWidget.setColumnCount(input_table_colunms)
self.tableWidget.setRowCount(input_table_rows)
self.tableWidget.setHorizontalHeaderLabels(input_table_header)
###======================给tablewidget设置行列表头============================
###================遍历表格每个元素,同时添加到tablewidget中========================
for i in range(input_table_rows):
input_table_rows_values = input_table.iloc[[i]]
#print(input_table_rows_values)
input_table_rows_values_array = np.array(input_table_rows_values)
input_table_rows_values_list = input_table_rows_values_array.tolist()[0]
#print(input_table_rows_values_list)
for j in range(input_table_colunms):
input_table_items_list = input_table_rows_values_list[j]
#print(input_table_items_list)
# print(type(input_table_items_list))
###==============将遍历的元素添加到tablewidget中并显示=======================
input_table_items = str(input_table_items_list)
newItem = QTableWidgetItem(input_table_items)
newItem.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
self.tableWidget.setItem(i, j, newItem)
###================遍历表格每个元素,同时添加到tablewidget中========================
else:
self.centralWidget.show()
if name == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
下面的代码
from PyQt5.Qt import * #刚开始学习可以这样一下导入
import sys,time
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QLineEdit 输出模式的案例")
self.resize(400,400)
self.set_ui()
self.i = 1
def set_ui(self):
self.account_lineEdit = QLineEdit(self)
self.pwd_lineEdit = QLineEdit(self)
self.btn = QPushButton(self)
def resizeEvent(self, event):
widget_w = 150
widget_h = 30
widget_h_margin = 10
top_margin = 50
self.account_lineEdit.resize(self.width()-60,widget_h)
self.pwd_lineEdit.resize(widget_w,widget_h)
self.btn.setText("登 录")
self.btn.resize(widget_w,widget_h)
x = (self.width() - widget_w)/2
y1 = top_margin
y2 = y1 + widget_h +widget_h_margin
y3 = y2 + widget_h +widget_h_margin
self.account_lineEdit.move(x,y1)
self.account_lineEdit.move(30,20)
self.pwd_lineEdit.move(x,y2)
self.btn.move(x,y3)
print(id(self.btn))
print(len(self.children()))
print("===",x,y1,y2,y3)
print("i = ",self.i)
self.i +=1
if name == "__main__":
app =QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
下面的代码正常
self.account_lineEdit.resize(self.width()-60,widget_h)