如何通过点击PyQt中的按钮将参数传递给函数?

时间:2011-07-21 23:31:08

标签: python function arguments pyqt4 argument-passing

我想在单击按钮时将参数传递给函数。我应该在此行button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))添加什么,以便将值传递给函数:

def calluser(name):
    print name

def Qbutton():
    button = QtGui.QPushButton("button",widget)
    name = "user"
    button.setGeometry(100,100, 60, 35)
    button.connect(button, QtCore.SIGNAL('clicked()'), calluser(name))

还有一件事,按钮将使用for循环生成;所以name值会有所不同。所以我想用按钮附上每个名字。我在Pytk中做了同样的事情,使用for循环并在单击时调用参数基函数。

6 个答案:

答案 0 :(得分:27)

通常,GUI是使用类构建的。通过使用绑定方法作为回调(请参阅下面的self.calluser),您可以通过self的属性(例如self.name)将信息“传递”到回调中:

例如,使用this tutorial中稍加修改的代码:

import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui

class QButton(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.button = QtGui.QPushButton('Button', self)
        self.name='me'
        self.button.clicked.connect(self.calluser)
    def calluser(self):
        print(self.name)

def demo_QButton():
    app = QtGui.QApplication(sys.argv)
    tb = QButton()
    tb.show()
    app.exec_()

if __name__=='__main__':
    demo_QButton()

由于总是在没有附加参数的情况下调用回调本身,当您需要将不同的附加信息传递给许多回调时,您需要为每个按钮进行不同的回调。

由于这可能很费力(如果手动完成),请改用函数工厂。请参阅下面的示例。功能工厂是一个封闭。它可以传递额外的参数,内部函数在调用时可以访问:

class ButtonBlock(QtGui.QWidget):

    def __init__(self, *args):
        super(QtGui.QWidget, self).__init__()
        grid = QtGui.QGridLayout()
        names = ('One', 'Two', 'Three', 'Four', 'Five',
                 'Six', 'Seven', 'Eight', 'Nine', 'Ten')
        for i, name in enumerate(names):
            button = QtGui.QPushButton(name, self)
            button.clicked.connect(self.make_calluser(name))
            row, col = divmod(i, 5)
            grid.addWidget(button, row, col)
        self.setLayout(grid)

    def make_calluser(self, name):
        def calluser():
            print(name)
        return calluser

app = QtGui.QApplication(sys.argv)
tb = ButtonBlock()
tb.show()
app.exec_()

答案 1 :(得分:8)

我尝试了一种有效的方法,这对我来说很有效。您可以使用以下代码:

from functools import partial

def calluser(name):
    print name

def Qbutton():
    button = QtGui.QPushButton("button",widget)
    name = "user"
    button.setGeometry(100,100, 60, 35)
    button.clicked.connect(partial(calluser,name))

答案 2 :(得分:3)

这是另一种方式。 --- PARTIAL - 我发现这个最简单:

    widget = QWidget()
    widgetLayout = QVBoxLayout()

    for action in list:

        button = QPushButton("{action}".format(action=action['name']),self)
        button.clicked.connect(partial(self.action_selected,action=action['name']))
        widgetLayout.addWidget(button)

    widget.setLayout(widgetLayout)

def action_selected(self,action):
    print action

发现于: http://tech-artists.org/forum/showthread.php?3118-PyQt-Maya-How-to-pass-arguments-to-a-function-when-connecting-it-to-PyQt-button

答案 3 :(得分:0)

下面显示的代码说明了将数据与生成的按钮相关联的方法。例如,您可以更改语句self.keydata[b]以将数据元组存储到assets,以便稍后处理按钮事件时使用。

请注意,在以下代码中,processButton(self)是包含按钮标题的先前定义的字典。在self.sender()例程中,buttons[]等于类变量class Tab5(QtGui.QWidget): buttons, keydata = {}, {} def __init__(self, fileInfo, parent=None): super(Tab5, self).__init__(parent) layout = QtGui.QVBoxLayout() for key in sorted(assets): b = self.buttons[key] = QtGui.QPushButton(assets[key], self) b.clicked.connect(self.processButton) layout.addWidget(b) print 'b[key]=',b, ' b-text=',assets[key] self.keydata[b] = key layout.addStretch(1) self.setLayout(layout) def processButton(self): print 'sender=',self.sender(), ' s-text=',self.sender().text(), ' data[.]=', self.keydata[self.sender()] pass 中的条目。

for

输出如下所示,其中在b[key]= <PySide.QtGui.QPushButton object at 0x7f382f2ca830> b-text= K1 b[key]= <PySide.QtGui.QPushButton object at 0x7f382f2ca908> b-text= K2 b[key]= <PySide.QtGui.QPushButton object at 0x7f382f2ca950> b-text= K3 b[key]= <PySide.QtGui.QPushButton object at 0x7f382f2ca998> b-text= K4 sender= <PySide.QtGui.QPushButton object at 0x7f382f2ca830> s-text= K1 data[.]= L1 sender= <PySide.QtGui.QPushButton object at 0x7f382f2ca908> s-text= K2 data[.]= L2 sender= <PySide.QtGui.QPushButton object at 0x7f382f2ca950> s-text= K3 data[.]= L3 sender= <PySide.QtGui.QPushButton object at 0x7f382f2ca998> s-text= K4 data[.]= L4 循环期间打印前四行,按顺序按下四个按钮时打印最后四行。

   package com.felix.tgp;

/**
 * Created by Felix on 4/15/2016.
 */

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainList extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    // Log tag
    private static final String TAG = MainList.class.getSimpleName();

    // Movies json url
    private static final String url = "http://....";
    private ProgressDialog pDialog;
    private List<Movie> movieList = new ArrayList<Movie>();
    private ListView listView;
    private CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, movieList);
        listView.setAdapter(adapter);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
/*
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position) {

                ItemClicked item = adapter.getItemAtPosition(position);

                Intent intent = new Intent(MainList.this, destinationActivity.class);
                //based on item add info to intent
                startActivity(intent);

            }


        });*/

        pDialog = new ProgressDialog(this);
        // Showing progress dialog before making http request
        pDialog.setMessage("Loading...");
        pDialog.show();


        // Creating volley request obj
        JsonArrayRequest movieReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        hidePDialog();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Movie movie = new Movie();
                                movie.setTitle(obj.getString("name"));
                                movie.setThumbnailUrl(obj.getString("images1"));
                                //movie.setDescribe(obj.getString("describe"));
                                //movie.setRating(((Number) obj.get("rating"))
                                //        .doubleValue());
                                movie.setYear(obj.getInt("id"));
                                movie.setTipe(obj.getString("tipe"));

                                /*// Genre is json array
                                JSONArray genreArry = obj.getJSONArray("genre");
                                ArrayList<String> genre = new ArrayList<String>();
                                for (int j = 0; j < genreArry.length(); j++) {
                                    genre.add((String) genreArry.get(j));
                                }
                                movie.setGenre(genre);*/

                                // adding movie to movies array
                                movieList.add(movie);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                hidePDialog();

            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(movieReq);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_calculator) {
            // Handle the camera action
            Intent calculator = new Intent(MainList.this, Calculator.class);
            startActivity(calculator);
        } else if (id == R.id.nav_chest) {
            Intent List = new Intent(MainList.this, MainList.class);
            startActivity(List);
        } else if (id == R.id.nav_back) {

        } else if (id == R.id.nav_shoulder) {

        } else if (id == R.id.nav_arm) {

        } else if (id == R.id.nav_abdominal) {

        } else if (id == R.id.nav_leg) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

答案 4 :(得分:0)

在Python中,类实例是可调用的。您可以只使用类的实例作为函数。该课程可以包含您想要的任何内容。 (在某些语言或框架中,可调用对象称为functor or a function object。)

class CallUser:
    def __init__(self, name):
        self.name = name
    def __call__(self):
        print(self.name)

def Qbutton():
    button = QtGui.QPushButton("button",widget)
    name = "user"
    button.setGeometry(100,100, 60, 35)
    button.clicked.connect(CallUser(name))
    # Object of type CallUser will work as a function!

答案 5 :(得分:0)

您可以简单地写

name = "user"
button.clicked.connect(lambda: calluser(name))
相关问题