简体   繁体   English

PySide QLineEdit 没有出现

[英]PySide QLineEdit not appearing

I am learning PySide and I can't seem to get QLineEdit to appear.我正在学习 PySide,我似乎无法让QLineEdit出现。 My best guess is because the QTextEdit is set as as the main widget and is covering it up.我最好的猜测是因为QTextEdit被设置为主要小部件并覆盖了它。 Here is my code:这是我的代码:

#!/usr/bin/python

import sys
from PySide import QtGui

class Window(QtGui.QMainWindow):
  def __init__(self):
    super(Window, self).__init__()
    self.initUI()

  def initUI(self):
    self.setWindowTitle("A Text Editor")
    self.setGeometry(300,1000,500,500)
    self.fileName = QtGui.QLineEdit('File Name')    
    self.fileName.resize(self.fileName.sizeHint())

    self.text = QtGui.QTextEdit()
    self.setCentralWidget(self.text)

    menubar = self.menuBar()
    exitAction = QtGui.QAction(QtGui.QIcon('exit.png'),'&Exit',self)
    exitAction.setShortcut('Ctrl+Q')
    exitAction.triggered.connect(self.close)
    saveAction = QtGui.QAction(QtGui.QIcon('save.png'),'&Save',self)
    saveAction.setShortcut('Ctrl+S')
    saveAction.triggered.connect(self.save)

    fileMenu = menubar.addMenu('&File')
    fileMenu.addAction(exitAction)
    fileMenu.addAction(saveAction)
    self.show()

  def save(self):
    f = open(self.fileName.text(),'w')
    f.write(self.text.toPlainText())
    f.close()

def main():
  app = QtGui.QApplication(sys.argv)
  win = Window()
  exit(app.exec_())

if __name__ == "__main__":
  main()

You're going to want to set a central widget with some form of layout to get them to both appear. 您将要设置具有某种布局形式的中央窗口小部件,以使其同时出现。

Something like: 就像是:

layout = QtGui.QVBoxLayout()
layout.addWidget(self.fileName)
layout.addWidget(self.text)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)

Done!完毕!

method 1方法一

Nothing is really wrong with your code, it's all about that size hint of QLineEdit你的代码没有什么问题,这都是关于 QLineEdit 的大小提示

( just disable that line with # ) (只需使用#禁用该行)

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import (QApplication, QTextEdit, QDialog, QLineEdit, QAction, QMainWindow)
#!/usr/bin/python

import sys

class Window(QMainWindow):
  def __init__(self):
    super(Window, self).__init__()
    self.initUI()

  def initUI(self):
    self.setWindowTitle("A Text Editor")
    self.setGeometry(300,1000,500,500)
    self.fileName = QLineEdit('File Name')    
    #self.fileName.resize(self.fileName.sizeHint())  # self.fileName.resize(self.fileName.sizeHint())

    self.text =QTextEdit()
    self.setCentralWidget(self.text)

    menubar = self.menuBar()
    self.show()

  def save(self):
    f = open(self.fileName.text(),'w')
    f.write(self.text.toPlainText())
    f.close()

if __name__ == "__main__":
  app = QApplication(sys.argv)
  win = Window()
  exit(app.exec_())

method 2方法二

create a container where the QLineEdit with some layout,... using _layout = QVBoxLayout()创建一个容器,其中 QLineEdit 具有一些布局,... 使用_layout = QVBoxLayout()

_layout.addWidget(self.fileName)
_layout.addWidget(self.text)

Try These Codes :试试这些代码:

#####################################################     1     #####################################################♀
from PyQt5.QtWidgets import *

class main(QMainWindow):
  def __init__(self):
    super().__init__()

    self.scroll = QScrollArea()
    self.widget = QTextEdit()
    self.scroll.setWidget(self.widget)
    self.scroll.setWidgetResizable(True)

    
    self.setCentralWidget(self.scroll)
    self.setWindowTitle("Text Editor")
    self.resize(700,600)
    self.show()


app1 = QApplication([])
window = main()
app1.exec()





#####################################################     2     #####################################################♀
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt


class main(QMainWindow):
  def __init__(self):
    super().__init__()

    self.widget = QWidget()
    self.vbox = QVBoxLayout(self.widget)
    self.label = QLabel("Text Editor")
    self.vbox.addWidget(self.label)
    self.vbox.addWidget(QTextEdit())

    self.label.setFont(QFont("Tahoma", 30))  
    self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)  
    
    self.setCentralWidget(self.widget)
    self.setWindowTitle("Text Editor")
    self.resize(700,600)
    self.show()


app2 = QApplication([])
window = main()
app2.exec()





#####################################################     3     #####################################################♀
from PyQt5.QtWidgets import *

class main(QTextEdit):
  def __init__(self):
    super().__init__()


    self.setWindowTitle("Text Editor")
    self.resize(700,600)
    self.show()


app3 = QApplication([])
window = main()
app3.exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM