简体   繁体   English

如何在python中同一类的不同功能的文本框中显示文本

[英]how to display the text in a textbox of different function of the same class in python

I am trying to get the date from a calendar widget to get printed into a textbox.我正在尝试从日历小部件中获取日期以打印到文本框中。 The calendar widget and the textbox are in the same class but the print function is in a different function.日历小部件和文本框在同一类中,但打印功能在不同的功能中。 all in the same class.都在同一个班级。

here is the code:这是代码:

from PyQt5 import QtCore, QtGui, QtWidgets
import calendar

I have the printDate function to print the date我有printDate函数来打印日期

class Ui_Calendar(object):
  def printDate(self, qDate):
    date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
    self.setupUi.textedit.setText(date)

Here in this function i have the calendar widget and the textbox:在这个函数中,我有日历小部件和文本框:

  def setupUi(self, MainWindow):

calendar widget:日历小部件:

    self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
    self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
    self.calendarWidget.setObjectName("calendarWidget")
    self.calendarWidget.clicked.connect(self.printDate)
    

textbox:文本框:

    self.textedit = QtWidgets.QLineEdit(self.centralwidget)
    self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
    self.textedit.setObjectName("textedit")

I get the calendar widget to open when button clicked.单击按钮时,我会打开日历小部件。 I also can get the date printed on python shell but cannot the date on the textbox.我也可以获取打印在 python shell 上的日期,但不能获取文本框上的日期。 As soon as i click the date on the calendar widget the program closes itself.只要我点击日历小部件上的日期,程序就会自行关闭。 i dont get any error msg.我没有收到任何错误消息。 I am new to programming and just started learning.我是编程新手,刚开始学习。 Please help me请帮我

here is my whole code:这是我的整个代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Calendar(object):
  def printDate(self, qDate):
    date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
    #print(date)
    self.setupUi.textedit.setText(self.date.toStrings())

def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(395, 310)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
    self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
    self.calendarWidget.setObjectName("calendarWidget")
    #self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
    self.calendarWidget.clicked.connect(self.printDate)
    
    MainWindow.setCentralWidget(self.centralwidget)
    
    self.textedit = QtWidgets.QLineEdit(self.centralwidget)
    self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
    self.textedit.setObjectName("textedit")
    #self.printDate(self.label.setText(date))
    
    
    
    
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    #self.label.setText(_translate("Mainwindow","date"))


 if __name__ == "__main__":
  import sys
  app = QtWidgets.QApplication(sys.argv)
  MainWindow = QtWidgets.QMainWindow()
  ui = Ui_Calendar()
  ui.setupUi(MainWindow)
  MainWindow.show()
  sys.exit(app.exec_())

Do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.不要修改 Qt Designer 生成的代码,而是创建另一个继承自适当小部件的类,并使用初始类来填充它。

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Calendar(object):
#    def printDate(self, qDate):
#        date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
#        #print(date)
#        self.setupUi.textedit.setText(self.date.toStrings())

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(395, 310)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
        self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
        self.calendarWidget.setObjectName("calendarWidget")
        #self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
#        self.calendarWidget.clicked.connect(self.printDate)
        
        MainWindow.setCentralWidget(self.centralwidget)
        
        self.textedit = QtWidgets.QLineEdit(self.centralwidget)
        self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
        self.textedit.setObjectName("textedit")
        #self.printDate(self.label.setText(date))
        
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        #self.label.setText(_translate("Mainwindow","date"))


class Calendar(QtWidgets.QMainWindow, Ui_Calendar):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.calendarWidget.clicked.connect(self.printDate)

    def printDate(self, qDate):
        date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
        
#        self.setupUi.textedit.setText(self.date.toStrings())
        self.textedit.setText(date)
        

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
#    MainWindow = QtWidgets.QMainWindow()
#    ui = Ui_Calendar()
#    ui.setupUi(MainWindow)
#    MainWindow.show()

    w = Calendar()
    w.show()
    sys.exit(app.exec_())

在此处输入图片说明

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

相关问题 如何在 Python 中忽略相同 div 但不同类中的文本 - How to ignore text in the same div but different class in Python python tkinter如何从类的函数中获取输出并将其显示在文本框中 - python tkinter how can I get output from function in a class and display it in textbox 如何在python中调用同一类的不同实例? - How to call a different instance of the same class in python? Python Beautifulsoup 从具有相同类的不同跨度中提取文本 - Python Beautifulsoup extract text from different span with same class 在同一类OpenCV Python中的两个不同函数中使用VideoCapture() - Use VideoCapture() in two different function within same class OpenCV Python python:如何将类函数结果分配给同一个类中的类变量 - python: How to assign a class function result to a class variable in the same class 如何在python(如SanctuaryRPG)中同时显示图像和文字? - How to display image and text at the same time in python (like SanctuaryRPG)? 如何在Python 3中显示用户输入的文本作为不同的字符? - How to display text the user inputs as a different character in Python 3? 如何杀死同一类中不同函数启动的子进程 - How to kill a subprocess initiated by a different function in the same class 如何使用Python从同一文本文件中分离出不同的输入格式 - How to separate different input formats from the same text file with Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM