按下按钮时无法点击/调用功能

时间:2020-02-15 14:27:31

标签: python python-3.x tkinter

我在一个较大的项目中添加了一个新窗口,但我在使用按钮时遇到了麻烦...当程序运行时单击它们时,没有迹象表明它们被按下并且什么也没有发生。我对tkinter来说还很陌生,所以如果有人可以帮助的话,将不胜感激!

{
  "general": {
    "knx_Gateway": "testing",
    "knx_Port": "testing",
    "knx_Medium": "testing",
    "knx_timezone": "testing"
  },
  "devices": [
    {
      "id": 0,
      "knx_ga": "test_1",
      "knx_dn": "test_1",
      "knx_dt": "test_1"
    },
    {
      "id": 1,
      "knx_ga": "test_2",
      "knx_dn": "test_2",
      "knx_dt": "test_2"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您将在到达打印声明之前返回。首先放置print('test'),然后再放置return True。当一个函数到达return时,它将结束该函数的执行,如果返回值被赋值为1,则返回该值,然后返回到代码中保留的位置。

工作示例:

from tkinter import *

def window():

    def sign_up_clicked():
        print ('test')
        # returns control to mainloop
        return True

    def log_in_clicked():
        print ('test')
        # returns control to mainloop
        return False

    window = Tk()
    window.title('Welcome')
    window.geometry('480x200')

    welcome_lbl = Label(window, text='Welcome', font=('Arial', 26), fg='#71C2FE') #Label with text saying 'welcome' in light blue 
    welcome_lbl.place(x=15, y=10) #placing welcome label in top left of sign_up_window

    under_line_lbl = Label(window, text='_______________________', font=('Arial', 35), fg='#FFB56B') #orange line under blue for looks
    under_line_lbl.place(x=0, y=40)

    intro_lbl = Label(window, text='Please press login if you already have an account,\notherwiseyou can create an account by pressing sign-up. ', font=('Arial', 14, 'bold italic'), justify='left')#lable with text underlined and italics
    intro_lbl.place(x=10, y=90)

    sign_up_btn = Button(window, text='Sign up', relief='raised', width=6, font=('candara', 14), command=sign_up_clicked)
    sign_up_btn.place(x=10, y=130)

    log_in_btn = Button(window, text='Login', relief='raised', width=6, font=('candara', 14), command=log_in_clicked)
    log_in_btn.place(x=10, y=160)

    window.mainloop()

window()