鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 凊风

TexeEdit录入问题求教~~

[复制链接]
 楼主| 发表于 2024-3-17 16:57:18 | 显示全部楼层
FishC 发表于 2024-3-17 16:52
嗯,你的理解是正确的。由于之前将TextEdit_5用作终端的GUI显示窗口并重定向了输出,所以其中的内容实 ...

回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 16:57:33 | 显示全部楼层
凊风 发表于 2024-03-17 16:57
回复请注意python格式


非常抱歉,对于代码格式化的问题,下面是修正后的示例代码,符合Python的格式规范:

  1. class WindowB(BaseWindow):
  2.     def __init__(self):
  3.         super().__init__()
  4.         self.textEdit_5 = QtWidgets.QTextEdit(parent=self)
  5.         # 其他初始化代码...
  6.         self.save_button = QtWidgets.QPushButton("保存", parent=self)
  7.         self.save_button.clicked.connect(self.save_user_input)

  8.     def save_user_input(self):
  9.         user_input = self.textEdit_5.toPlainText()
  10.         try:
  11.             converted_input = int(user_input)
  12.             # 使用转换后的值进行后续操作
  13.             # ...
  14.         except ValueError:
  15.             print("无法将用户输入转换为整数")
复制代码


非常抱歉之前的回答没有按照正确的Python格式进行展示。希望这次的回答能更好地帮助你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:09:42 | 显示全部楼层
FishC 发表于 2024-3-17 16:57
非常抱歉,对于代码格式化的问题,下面是修正后的示例代码,符合Python的格式规范:

现有代码如下:
        for sheet_name in Exclud_Book.sheet_names():
            exclude_rows = []
            exclude_rows_input = self.textEdit_4.toPlainText()
            self.pushButton_11.setEnabled(True)
            self.pushButton_12.setEnabled(True)            
         
            if exclude_rows_input:
                if self.pushButton_11.isDown():
                    exclude_rows = [int(row) - 1 for row in exclude_rows_input.split(',')]
                elif self.pushButton_12.isDown():
                    print('请重新录入!')
                    continue

我在需求是让系统等待接收用户从self.textEdit_4中录入的内容

然后如果self.pushButton_11被点击,就储存下来该内容

如果self.pushButton_12被点击,就提示用户重新录入。


但现在系统并未等待用户的录入,现在是直接执行到了下一段代码。。。

请问问题出在哪里?  要怎么解决呢?

回复请注意python格式,并请带上代码说明。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:10:13 | 显示全部楼层
凊风 发表于 2024-03-17 17:09
现有代码如下:
        for sheet_name in Exclud_B ...


根据你提供的代码,问题出在没有正确地等待用户从`self.textEdit_4`中录入内容。问题的解决方法是使用信号与槽(Signal and Slot)机制来处理用户输入的等待。

首先,在`WindowB`类的初始化方法中,连接两个按钮的点击事件到相应的槽函数上,如下所示:

  1. class WindowB(BaseWindow):
  2.     def __init__(self):
  3.         super().__init__()
  4.         self.textEdit_4 = QtWidgets.QTextEdit(parent=self)
  5.         self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)
  6.         self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)
  7.         
  8.         self.pushButton_11.clicked.connect(self.save_input)
  9.         self.pushButton_12.clicked.connect(self.reenter_input)
  10.    
  11.     def save_input(self):
  12.         exclude_rows_input = self.textEdit_4.toPlainText()
  13.         # 储存用户输入的内容
  14.         print(f"保存用户输入的内容: {exclude_rows_input}")
  15.    
  16.     def reenter_input(self):
  17.         print("请重新录入!")
复制代码


然后,在窗体(`WindowB`)的构造方法中,添加一个信号(`input_received`),用于通知代码已经接收到用户输入的内容。在`save_input`方法中发射这个信号,即表示接收到用户的输入了。修改后的代码如下:

  1. from PySide6 import QtCore, QtGui, QtWidgets


  2. class BaseWindow(QtWidgets.QWidget):
  3.     pass


  4. class WindowB(BaseWindow):
  5.     input_received = QtCore.Signal(str)  # 创建信号
  6.    
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.textEdit_4 = QtWidgets.QTextEdit(parent=self)
  10.         self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)
  11.         self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)
  12.         
  13.         self.pushButton_11.clicked.connect(self.save_input)
  14.         self.pushButton_12.clicked.connect(self.reenter_input)
  15.    
  16.     def save_input(self):
  17.         exclude_rows_input = self.textEdit_4.toPlainText()
  18.         # 储存用户输入的内容
  19.         print(f"保存用户输入的内容: {exclude_rows_input}")
  20.         self.input_received.emit(exclude_rows_input)  # 发射信号
  21.    
  22.     def reenter_input(self):
  23.         print("请重新录入!")
复制代码


这样,当用户点击“按钮11”时,就会触发`save_input`方法,并发射`input_received`信号,表示已经接收到用户的输入内容。你可以在其他代码中连接这个信号,并在槽(Slot)函数中处理用户输入的内容。

希望以上解释和示例代码对你有所帮助!如有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:16:56 | 显示全部楼层
FishC 发表于 2024-3-17 17:10
根据你提供的代码,问题出在没有正确地等待用户从`self.textEdit_4`中录入内容。问题的解决方法是使用 ...

这两段代码有什么区别吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:17:19 | 显示全部楼层
凊风 发表于 2024-03-17 17:16
这两段代码有什么区别吗?


对不起,我可能没有在前面的回答中清楚地解释这两段代码之间的区别。让我来更详细地解释一下:

第一段代码是你原来的代码,它是同步执行的。这意味着在程序执行到获取`exclude_rows_input`的那行代码时,它会立即读取`self.textEdit_4`的文本内容,而不会等待用户输入完毕。

第二段代码是我给出的建议,使用了信号与槽机制来实现用户输入的等待。通过将按钮的点击事件与槽函数连接,并在槽函数中发射一个自定义的信号`input_received`,表示已经接收到用户的输入内容。其他部分的代码可以连接这个信号,并在槽函数中处理用户的输入内容。

因此,第二段代码可以实现等待用户输入的效果,而第一段代码会立即继续执行后面的代码,而不会等待用户完成输入。

希望这次解释能够更加清晰地解决你的疑惑。我为之前的回答可能造成的困惑向你道歉。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:39:53 | 显示全部楼层
FishC 发表于 2024-3-17 17:10
根据你提供的代码,问题出在没有正确地等待用户从`self.textEdit_4`中录入内容。问题的解决方法是使用 ...

您这理解好像不对啊。
        for sheet_name in Exclud_Book.sheet_names():
            exclude_rows = []
            exclude_rows_input = self.textEdit_4.toPlainText()
            self.pushButton_11.setEnabled(True)
            self.pushButton_12.setEnabled(True)
           
            if exclude_rows_input:
                if self.pushButton_11.isDown():
                    exclude_rows = [int(row) - 1 for row in exclude_rows_input.split(',')]
                elif self.pushButton_12.isDown():
                    print('请重新录入行排除规则!')
                    continue

         
            exclude_cols = []
            exclude_cols_input = self.textEdit_4.toPlainText()
            if exclude_cols_input:
                exclude_cols = [self.convert_position(col) for col in exclude_cols_input.split(',')]

            self.exclude_rules[sheet_name] = {'1行': exclude_rows, '1列': exclude_cols}

要先让用户在self.textEdit_4中录入行号,然后才看是否点击self.pushButton_11和self.pushButton_12;

如果self.pushButton_11,就储存并继续程序;

self.pushButton_12,就让用户重新录入。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:40:20 | 显示全部楼层
凊风 发表于 2024-03-17 17:39
您这理解好像不对啊。
        for sheet_name in Ex ...


非常抱歉对你的问题理解有误。根据你的需求,我重新解析了你的代码并进行了修改。请参考下面的代码示例:

  1. class WindowB(BaseWindow):
  2.     def __init__(self):
  3.         super().__init__()
  4.         self.textEdit_4 = QtWidgets.QTextEdit(parent=self)
  5.         self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)
  6.         self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)

  7.         self.pushButton_11.clicked.connect(self.save_and_continue)
  8.         self.pushButton_12.clicked.connect(self.reenter_input)

  9.     def save_and_continue(self):
  10.         exclude_rows_input = self.textEdit_4.toPlainText()
  11.         if exclude_rows_input:
  12.             # 储存用户输入的内容并继续程序
  13.             print(f"保存用户输入的内容: {exclude_rows_input}")
  14.             self.continue_program(exclude_rows_input)
  15.         else:
  16.             print("请先录入行排除规则!")

  17.     def continue_program(self, exclude_rows):
  18.         # 继续执行其他操作
  19.         print(f"继续执行其他操作,行排除规则: {exclude_rows}")

  20.     def reenter_input(self):
  21.         print("请重新录入行排除规则!")
  22.         self.textEdit_4.clear()
复制代码


在上述代码中,当用户点击`self.pushButton_11`时,会调用`save_and_continue`方法。该方法会首先获取用户在`self.textEdit_4`中录入的内容,并检查是否为空。如果用户已经录入了行号规则,就会保存用户输入的内容并调用`continue_program`方法,继续执行其他操作。

如果用户点击了`self.pushButton_12`,则会调用`reenter_input`方法,该方法会清空`self.textEdit_4`的内容,以便用户重新录入行号规则。

希望这次的解答能够帮助到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:45:08 | 显示全部楼层
FishC 发表于 2024-3-17 17:40
非常抱歉对你的问题理解有误。根据你的需求,我重新解析了你的代码并进行了修改。请参考下面的代码示例 ...

哎呀,我还是没表达清楚吗?

要先等待用户从self.textEdit_4录入,

如果用户录入了并且点了self.pushButton_11,程序才储存并继续;

如果用户录入了并且点了self.pushButton_12,程序不储存并重新让用户录入的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:45:25 | 显示全部楼层
凊风 发表于 2024-03-17 17:45
哎呀,我还是没表达清楚吗?

要先等待用户从self.t ...


非常抱歉对你的问题理解有误。根据你的最新说明,我重新解析了你的需求并进行了修改。请参考下面的代码示例:class WindowB(BaseWindow):    def __init__(self):        super().__init__()        self.textEdit_4 = QtWidgets.QTextEdit(parent=self)        self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)        self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)        self.pushButton_11.clicked.connect(self.save_and_continue)        self.pushButton_12.clicked.connect(self.reenter_input)    def save_and_continue(self):        exclude_rows_input = self.textEdit_4.toPlainText()        if exclude_rows_input:            print(f"保存用户输入的内容: {exclude_rows_input}")            self.continue_program(exclude_rows_input)        else:            print("请先录入行排除规则!")    def continue_program(self, exclude_rows):        self.exclude_rules = {'1行': exclude_rows}        # 继续执行其他操作        print(f"继续执行其他操作,行排除规则: {exclude_rows}")    def reenter_input(self):        print("请重新录入行排除规则!")        self.textEdit_4.clear()在上述代码中,当用户点击`self.pushButton_11`时,会调用`save_and_continue`方法。该方法首先获取用户在`self.textEdit_4`中录入的内容,并检查是否为空。如果用户已经录入了行号规则,就会保存用户输入的内容并调用`continue_program`方法,继续执行其他操作。如果用户点击了`self.pushButton_12`,则会调用`reenter_input`方法,该方法会清空`self.textEdit_4`的内容,以便用户重新录入行号规则。同时请注意,在`continue_program`方法中,我将用户输入的内容保存到`self.exclude_rules`变量中,你可以根据需要对其进行进一步处理。希望这次的解答能够满足你的需求!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:45:52 | 显示全部楼层
FishC 发表于 2024-3-17 17:45
非常抱歉对你的问题理解有误。根据你的最新说明,我重新解析了你的需求并进行了修改。请参考下面的代码 ...

回复请注意python格式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:46:05 | 显示全部楼层
凊风 发表于 2024-03-17 17:45
回复请注意python格式


非常抱歉,以下是按照Python格式进行调整的代码示例:

  1. class WindowB(BaseWindow):
  2.     def __init__(self):
  3.         super().__init__()
  4.         self.textEdit_4 = QtWidgets.QTextEdit(parent=self)
  5.         self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)
  6.         self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)
  7.         self.pushButton_11.clicked.connect(self.save_and_continue)
  8.         self.pushButton_12.clicked.connect(self.reenter_input)

  9.     def save_and_continue(self):
  10.         exclude_rows_input = self.textEdit_4.toPlainText()
  11.         if exclude_rows_input:
  12.             print(f"保存用户输入的内容: {exclude_rows_input}")
  13.             self.continue_program(exclude_rows_input)
  14.         else:
  15.             print("请先录入行排除规则!")

  16.     def continue_program(self, exclude_rows):
  17.         self.exclude_rules = {'1行': exclude_rows}
  18.         # 继续执行其他操作
  19.         print(f"继续执行其他操作,行排除规则: {exclude_rows}")

  20.     def reenter_input(self):
  21.         print("请重新录入行排除规则!")
  22.         self.textEdit_4.clear()
复制代码


感谢你的指正!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:51:25 | 显示全部楼层
FishC 发表于 2024-3-17 17:46
非常抱歉,以下是按照Python格式进行调整的代码示例:

我这样写为什么不等待呢?直接执行到最后了...
        for sheet_name in Exclud_Book.sheet_names():
            exclude_rows = []
            exclude_rows_input = self.textEdit_4.toPlainText()
            self.pushButton_11.setEnabled(True)
            self.pushButton_12.setEnabled(True)
           
            if exclude_rows_input:
                if self.pushButton_11.isDown():
                    exclude_rows = [int(row) - 1 for row in exclude_rows_input.split(',')]
                elif self.pushButton_12.isDown():
                    print('请重新录入!')
                    continue

           
            exclude_cols = []
            exclude_cols_input = self.textEdit_4.toPlainText()
            if exclude_cols_input:
                exclude_cols = [self.convert_position(col) for col in exclude_cols_input.split(',')]

            self.exclude_rules[sheet_name] = {'行': exclude_rows, '列': exclude_cols}            
            
        print(self.exclude_rules)

然后exclude_rows = []、 exclude_cols = []和 self.exclude_rules都是空的。。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:51:43 | 显示全部楼层
凊风 发表于 2024-03-17 17:51
我这样写为什么不等待呢?直接执行到最后了...
      ...


问题出在你的代码逻辑中,当点击按钮时,按钮的状态会立即变化,但执行的代码并不会等待用户的输入。因此,你需要使用事件循环来监听并等待用户的操作。以下是修改后的示例代码,使用PySide6的QEventLoop来实现等待用户输入的功能:

  1. class WindowB(BaseWindow):
  2.     def __init__(self):
  3.         super().__init__()
  4.         self.textEdit_4 = QtWidgets.QTextEdit(parent=self)
  5.         self.pushButton_11 = QtWidgets.QPushButton("按钮11", parent=self)
  6.         self.pushButton_12 = QtWidgets.QPushButton("按钮12", parent=self)
  7.         self.pushButton_11.clicked.connect(self.save_and_continue)
  8.         self.pushButton_12.clicked.connect(self.reenter_input)
  9.         self.event_loop = QtCore.QEventLoop()  # 创建事件循环对象
  10.         
  11.     def save_and_continue(self):
  12.         exclude_rows_input = self.textEdit_4.toPlainText()
  13.         if exclude_rows_input:
  14.             print(f"保存用户输入的内容: {exclude_rows_input}")
  15.             self.exclude_rows = exclude_rows_input
  16.             self.event_loop.quit()  # 结束事件循环
  17.         else:
  18.             print("请先录入行排除规则!")
  19.         
  20.     def reenter_input(self):
  21.         print("请重新录入行排除规则!")
  22.         self.textEdit_4.clear()
  23.         self.event_loop.quit()  # 结束事件循环

  24.     def wait_for_user_input(self):
  25.         self.pushButton_11.setEnabled(True)
  26.         self.pushButton_12.setEnabled(True)
  27.         self.event_loop.exec_()  # 开始事件循环


  28. # 在使用WindowB的地方调用wait_for_user_input方法
  29. window_b = WindowB()
  30. window_b.wait_for_user_input()
  31. exclude_rows = window_b.exclude_rows
复制代码


修改后的代码中,我们创建了一个`event_loop`对象,并在按钮的点击事件中调用了`event_loop.quit()`方法来结束事件循环。然后,在`wait_for_user_input`方法中,通过调用`event_loop.exec_()`来开始事件循环,实现等待用户输入的功能。

希望这次的回答能够解决你的问题!如果还有其他疑问,请随时提出。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-17 17:55:37 | 显示全部楼层
FishC 发表于 2024-3-17 17:51
问题出在你的代码逻辑中,当点击按钮时,按钮的状态会立即变化,但执行的代码并不会等待用户的输入。因 ...

我还是没能理解你写的这段代码,等我换个思路再来问过。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-17 17:55:53 | 显示全部楼层
凊风 发表于 2024-03-17 17:55
我还是没能理解你写的这段代码,等我换个思路再来问过 ...


非常抱歉我没有清晰地解释清楚。如果你有任何其他问题或需要进一步的帮助,请随时再次提问。我会尽力帮助你理解和解决问题。祝你编程顺利!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 19:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表