共計 881 個字符,預計需要花費 3 分鐘才能閱讀完成。
要獲取一個視圖控件的內容,可以使用以下方法之一:
- 使用
tkinter
庫:
import tkinter as tk
root = tk.Tk()
def get_content():
content = entry.get()
print(content)
entry = tk.Entry(root)
entry.pack()
button = tk.Button(root, text="Get Content", command=get_content)
button.pack()
root.mainloop()
在上述示例中,我們創建了一個 Entry
小部件,用戶可以在其中輸入文本。然后,我們創建了一個按鈕,當用戶點擊該按鈕時,將調用 get_content
函數。該函數獲取 Entry
小部件的內容,并將其打印到控制臺。
- 使用
PyQt
庫:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton
def get_content():
content = line_edit.text()
print(content)
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
line_edit = QLineEdit()
layout.addWidget(line_edit)
button = QPushButton("Get Content")
button.clicked.connect(get_content)
layout.addWidget(button)
window.setLayout(layout)
window.show()
app.exec_()
在上述示例中,我們創建了一個 QLineEdit
小部件,用戶可以在其中輸入文本。然后,我們創建了一個按鈕,當用戶點擊該按鈕時,將調用 get_content
函數。該函數獲取 QLineEdit
小部件的內容,并將其打印到控制臺。
這些示例中的方法可以根據你使用的 GUI 庫和控件類型進行適當的修改。
丸趣 TV 網 – 提供最優質的資源集合!
正文完