问题使用tkinter按钮

时间:2018-04-23 14:21:25

标签: python tkinter

目前正在尝试使用tkinter创建一个简单的登录gui,并且我遇到了按钮的问题。这是我的代码:

public class hiddenNetwork extends  Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private WebView mWebview;
    ProgressDialog prDialog;


    public hiddenNetwork() {
        // Required empty public constructor
    }


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };
    public static hiddenNetwork newInstance(String param1, String param2) {
        hiddenNetwork fragment = new hiddenNetwork();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }
    int count=0;
    private void webViewGoBack(){
        count++;
        mWebview.goBack();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_announcement, container, false);

        mWebview = (WebView)view.findViewById(R.id.webView);

        CookieManager cookieManager = CookieManager.getInstance();

        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);









        mWebview.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                DownloadManager.Request request = new DownloadManager.Request(
                        Uri.parse(url));

                request.allowScanningByMediaScanner();
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
                DownloadManager dm = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
                dm.enqueue(request);
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
                intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
                intent.setType("*/*");//any application,any extension

            }
        });

        mWebview.setWebViewClient(new WebViewClient() {


            public boolean shouldOverrideUrlLoading (WebView view, String url) {
                if (url.endsWith(".pdf")) {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    // if want to download pdf manually create AsyncTask here
                    // and download file
                    return true;
                }
                return false;
            }






            @Override public void onReceivedError(WebView view, WebResourceRequest request,
                                                  WebResourceError error) {
                super.onReceivedError(view, request, error);

            }
        });


        mWebview.loadUrl("https://login.microsoftonline.com");

        return view;
    }







    public void myOnKeyDown(int key_code){
        //do whatever you want here
    }
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {

        void onFragmentInteraction(Uri uri);
    }

}

每当我运行此代码时,我都会收到此错误:

from tkinter import *
from requesting_internet import login


window = Tk()


def login_button_function():
    username = Username_entry.get()
    password = Password_entry.get()
    print(login(username, password))

Login_label = Label(window, text="Login:")
Login_label.grid(row=0, column=1)
Username_label = Label(window, text="Username")
Username_label.grid(row=1, column=0)
Username_entry = Entry(window, bd=5)
Username_entry.grid(row=1, column=1, columnspan=2)
Password_label = Label(window, text="Password")
Password_label.grid(row=2, column=0)
Password_entry = Entry(window, bd=5, show="*")
Password_entry.grid(row=2, column=1, columnspan=2)
Login_button = Button(window, text="Login")
Login_button.grid(row=3, column=1, command = login_button_function)

window.mainloop()

我无法在任何地方找到解决此问题的任何内容。

1 个答案:

答案 0 :(得分:2)

您正在引用一个命令参数,一个用于按钮的参数,在网格放置函数中,它没有命令参数,在创建按钮时将命令移动到它应该没问题。