2017-04-06 97 views
1

我似乎無法弄清楚我在PyQt中如何做到這一點。它使單選按鈕,更像是按鈕(這是Tkinter的)
enter image description here如何在PyQt中製作像單選按鈕的按鈕?

Radiobutton(root, 
       text=txt, 
       indicatoron = 0, 
       width = 20, 
       padx = 20, 
       variable=v, 
       command=ShowChoice, 
       value=val).pack(anchor=W) 
+0

我不這麼認爲,你必須給他們自己設計,也可以簡單地使用按鈕與一些單選按鈕邏輯。這將不難實現。 –

+0

你在做什麼和你期望的不同?你的代碼對我來說看起來不錯,儘管你顯然只顯示一個按鈕。 –

回答

2

你爲什麼不使用QButtonGroup?它是默認情況下的獨佔功能,可幫助您在單擊某個選項時跟蹤和響應事件。

代碼示例:

from PyQt5.QtWidgets import * 

app = QApplication([]) 

w = QWidget() 
w.setWindowTitle('pyqt') 
l = QVBoxLayout(w) 
l.setContentsMargins(0, 0, 0, 0) 
l.addWidget(QLabel('Choose your favorite programming language:')) 

titles = ['Python', 'Perl', 'Java', 'C++', 'C'] 
buttons = [QPushButton(title) for title in titles] 

button_group = QButtonGroup() 

for button in buttons: 
    l.addWidget(button) 
    button_group.addButton(button) 
    button.setCheckable(True) 

w.show() 
app.exec() 

它看起來像除了在風格(使用Qt stylesheets爲)差異的例子。

enter image description here

+0

謝謝你,我正在尋找什麼! –