下面的代码是在选择3个按钮中的任何一个时更改背景颜色:红色,绿色或蓝色.当我选择其中任何一个时,实际上什么也没发生.但是,从JButtons更改为JRadioButtons或JToggleButtons确实有效.谁知道为什么?是因为JButton.isselected()方法被窃听并且它总是返回false?我感谢任何帮助......谢谢.
public class bgcolor2 extends JFrame
{
private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT= 400;
private ActionListener listener;
private JButton greenbutton;
private JButton redbutton;
private JButton bluebutton;
private JPanel colorpanel;
private JPanel buttonpanel;
public bgcolor2()
{
colorpanel = new JPanel();
add(colorpanel,BorderLayout.CENTER);
class bgcolorlistener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
changebgcolor();
}
}
listener=new bgcolorlistener();
createbuttons();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
public void createbuttons()
{
greenbutton = new JButton("Green");
greenbutton.addActionListener(listener);
bluebutton = new JButton("Blue");
bluebutton.addActionListener(listener);
redbutton = …Run Code Online (Sandbox Code Playgroud) 当我运行下面的代码时,它显示如下。为什么 x 不是“x”而是变成布尔值?这种情况仅发生在传递到用 lambda 调用的函数中的第一个参数上。
假 y /home/me/model/some_file
from PyQt5.QtWidgets import QPushButton
modelpath = '/home/me/model'
filelist = os.listdir(modelpath)
x = 'x'
y = 'y'
def HelloWidget(QWidget):
def __init__(self):
for file in filelist:
button = QPushButton(file)
button.clicked.connect(lambda x=x,y=y,file=file: self.myfunction(x,y,file)
def myfunction(self,x,y,file):
print(x)
print(y)
print(file)
Run Code Online (Sandbox Code Playgroud)