如何正确附加按钮链接?

时间:2015-10-07 16:39:28

标签: python tkinter

我试图将链接附加到点击时会打开的按钮。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>



char *strstrnc(const char *str1, const char *str2);

int main()
{
    char buf1[80],buf2[80];

    printf("Enter the first string to be compared: ");
    gets(buf1);
    printf("Enter the second string to be compared: ");
    gets(buf2);

    strstrnc(buf1,buf2);

    return 0;
}


char *strstrnc(const char *buf1, const char *buf2)
{
    char *p1, *ptr1, *p2, *ptr2, *loc;
    int ctr;

    ptr1 = malloc(80 * sizeof(char));
    ptr2 = malloc(80 * sizeof(char));


    p1 = ptr1;
    p2 = ptr2;



    for (ctr = 0; ctr < strlen(buf1);ctr++)
    {
        *p1++ = tolower(buf1[ctr]);
    }
    *p1 = '\0';

    for (ctr = 0; ctr < strlen(buf2);ctr++)
    {
        *p2++ = tolower(buf2[ctr]);
    }
    *p2 = '\0';

    printf("The new first string is %s.\n", ptr1);
    printf("The new first string is %s.\n", ptr2);

    loc = strstr(ptr1,ptr2);
    if  (loc == NULL)
    {
        printf("No match was found!\n");
    }
    else
    {
        printf("%s was found at position %ld.\n", ptr2, loc-ptr1);
    }

    return loc;

}

当我运行它时,它会在弹出框之前自动打开链接。如何编写它以便链接仅在按下按钮时运行? 我会创建另一个函数来打开链接,然后命令它在按下按钮时使用该功能吗?

2 个答案:

答案 0 :(得分:0)

我将按钮命令包含在lambda函数中。所以我的按钮看起来像这样:

`_Button = Button(self, text="Don't Click", command=lambda: webbrowser.open("http://example.com", new=2, autoraise="True"))`

答案 1 :(得分:0)

根据经验,按钮命令应该调用不接受任何参数的函数。通过这样做,编写命令,创建按钮和调试程序变得更加容易。

所以,在这种情况下,我会创建一个名为open_browseron_dont_click_button的函数,或类似的东西:

def open_browser(self):
    webbrowser.open("http://example.com", new=2, autoraise="True")

然后,按钮变得更容易实现:

_Button = Button(..., command=open_browser, ...)  
相关问题