def photo_class(self): print(1) self.pushButton_6.clicked.connect(self.photo_craw) print(2) def photo_craw(self): directory = filedialog.askdirectory() if directory: print(f"Selected directory: {directory}")删去窗口
时间: 2024-03-11 15:50:00 浏览: 59
你可以直接将 `photo_craw` 函数中的内容提取出来,然后在 `photo_class` 函数中调用 `filedialog.askdirectory()` 来选择目录并处理选择结果。以下是示例代码:
```python
from tkinter import filedialog
def photo_class(self):
print(1)
self.pushButton_6.clicked.connect(self.photo_craw)
print(2)
def photo_craw(self):
directory = filedialog.askdirectory()
if directory:
print(f"Selected directory: {directory}")
# 在这里可以处理选择的目录
```
这个程序中,`photo_class` 函数没有改变,`photo_craw` 函数被删去了,而是在 `photo_class` 函数中调用了 `filedialog.askdirectory()` 来选择目录并处理选择结果。
相关问题
import sys from PyQt5.QtWidgets import QApplication, QWidget , QMainWindow from Calui1 import Ui_Form class Mywindow(QWidget,Ui_Form): def __init__(self): super(Mywindow,self).__init__() self.setupUi(self) def btnClear_clicked(self): self.lineEdit.clear() def btn1(self): self.lineEdit.insert("1") def btn2(self): self.lineEdit.insert("2") def btn3(self): self.lineEdit.insert("3") def btn4(self): self.lineEdit.insert("4") def btn5(self): self.lineEdit.insert("5") def btn6(self): self.lineEdit.insert("6") def btn7(self): self.lineEdit.insert("7") def btn8(self): self.lineEdit.insert("8") def btn9(self): self.lineEdit.insert("9") def btn0(self): self.lineEdit.insert("0") def btn_plus(self): self.lineEdit.insert("+") def btn_sub(self): self.lineEdit.insert("-") def btn_mul(self): self.lineEdit.insert("*") def btn_div(self): self.lineEdit.insert("/") def btnequal(self): text=self.lineEdit.text() self.lineEdit.insert("= %.2f" %(eval(text))) if __name__=='__main__': app=QApplication(sys.argv) win=Mywindow() win.pushButton_7.clicked.connect(win.btnClear_clicked()) win.pushButton.clicked.connect(win.btn1()) win.pushButton_2.clicked.connect(win.btn2()) win.pushButton_3.clicked.connect(win.btn3()) win.pushButton_4.clicked.connect(win.btn_plus()) win.pushButton_5.clicked.connect(win.btn4()) win.pushButton_6.clicked.connect(win.btn7()) win.pushButton_8.clicked.connect(win.btn5()) win.pushButton_9.clicked.connect(win.btn8()) win.pushButton_10.clicked.connect(win.btn0()) win.pushButton_11.clicked.connect(win.btn6()) win.pushButton_12.clicked.connect(win.btn9()) win.pushButton_13.clicked.connect(win.btnequal()) win.pushButton_14.clicked.connect(win.btn_sub()) win.pushButton_15.clicked.connect(win.btn_mul()) win.pushButton_16.clicked.connect(win.btn_div()) win.show() sys.exit(app.exec_())错在哪
你忘记了在导入模块时使用逗号分隔,应该改为:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from Calui1 import Ui_Form
class Mywindow(QWidget,Ui_Form):
def __init__(self):
super(Mywindow,self).__init__()
self.setupUi(self)
def btnClear_clicked(self):
self.lineEdit.clear()
def btn1(self):
self.lineEdit.insert("1")
def btn2(self):
self.lineEdit.insert("2")
def btn3(self):
self.lineEdit.insert("3")
def btn4(self):
self.lineEdit.insert("4")
def btn5(self):
self.lineEdit.insert("5")
def btn6(self):
self.lineEdit.insert("6")
def btn7(self):
self.lineEdit.insert("7")
def btn8(self):
self.lineEdit.insert("8")
def btn9(self):
self.lineEdit.insert("9")
def btn0(self):
self.lineEdit.insert("0")
def btn_plus(self):
self.lineEdit.insert("+")
def btn_sub(self):
self.lineEdit.insert("-")
def btn_mul(self):
self.lineEdit.insert("*")
def btn_div(self):
self.lineEdit.insert("/")
def btnequal(self):
text=self.lineEdit.text()
self.lineEdit.insert("= %.2f" %(eval(text)))
if __name__=='__main__':
app=QApplication(sys.argv)
win=Mywindow()
win.pushButton_7.clicked.connect(win.btnClear_clicked)
win.pushButton.clicked.connect(win.btn1)
win.pushButton_2.clicked.connect(win.btn2)
win.pushButton_3.clicked.connect(win.btn3)
win.pushButton_4.clicked.connect(win.btn_plus)
win.pushButton_5.clicked.connect(win.btn4)
win.pushButton_6.clicked.connect(win.btn7)
win.pushButton_8.clicked.connect(win.btn5)
win.pushButton_9.clicked.connect(win.btn8)
win.pushButton_10.clicked.connect(win.btn0)
win.pushButton_11.clicked.connect(win.btn6)
win.pushButton_12.clicked.connect(win.btn9)
win.pushButton_13.clicked.connect(win.btnequal)
win.pushButton_14.clicked.connect(win.btn_sub)
win.pushButton_15.clicked.connect(win.btn_mul)
win.pushButton_16.clicked.connect(win.btn_div)
win.show()
sys.exit(app.exec_())
```
def init_slots(self): self.pushButton_img.clicked.connect(self.load_source) self.pushButton_model.clicked.connect(self.select_model) self.pushButton_detect.clicked.connect(self.target_detect) self.pushButton_showdir.clicked.connect(self.show_dir) self.pushButton_camera_detect.clicked.connect(self.camera_detect)
这段代码是用于初始化信号槽(slots)的连接。
通过调用`clicked.connect`方法,将按钮的`clicked`信号与相应的槽函数进行连接。具体来说:
- `self.pushButton_img.clicked.connect(self.load_source)`将"pushButton_img"按钮的`clicked`信号连接到`load_source`槽函数。
- `self.pushButton_model.clicked.connect(self.select_model)`将"pushButton_model"按钮的`clicked`信号连接到`select_model`槽函数。
- `self.pushButton_detect.clicked.connect(self.target_detect)`将"pushButton_detect"按钮的`clicked`信号连接到`target_detect`槽函数。
- `self.pushButton_showdir.clicked.connect(self.show_dir)`将"pushButton_showdir"按钮的`clicked`信号连接到`show_dir`槽函数。
- `self.pushButton_camera_detect.clicked.connect(self.camera_detect)`将"pushButton_camera_detect"按钮的`clicked`信号连接到`camera_detect`槽函数。
通过这些连接,当用户点击相应的按钮时,对应的槽函数将会被调用。
阅读全文