python - 我的 tkinter 按钮命令功能有问题

标签 python function button tkinter command

当我运行时,我制作了 Facebook 脚本,他给了我错误 hi is not Defined 当我禁用 Tkinter 并使用 power shell 启动时,他没有启动,他工作正常,请帮助我!我正在使用 python 3 这是我的错误屏幕截图 我在运行时使用 Tkinter 创建了这个 GUI 他给了我一个错误并且没有启动

enter image description here

Python 代码

import pyautogui as pg
import time
from selenium import webdriver
from getpass import getpass
from tkinter import *

window = Tk()
window.title('Facebook Sharing')
text = Label(window, text='Facebook Sharing Script', font="Source_Sans_Pro 20 bold")
text.pack()
frame = Frame(window)
frame.pack()

one = Frame(window)
one.pack( side = TOP )

frontframe = Frame(window)
frontframe.pack( side = TOP )


bottomframe = Frame(window)
bottomframe.pack( side = TOP )

lastframe = Frame(window)
lastframe.pack( side = TOP )

last = Frame(window)
last.pack( side = TOP )

Lable1 = Label(frame, text="Enter Your UserName!    ", font='Source_Sans_Pro 11')
Lable1.pack( side = LEFT)
user_name = Entry(frame, bd =5)
user_name.pack(side = RIGHT)

Lable2 = Label(frontframe, text="Enter Your Passowrd!     ", font='Source_Sans_Pro 11')
Lable2.pack( side = LEFT)
Password = Entry(frontframe, bd =5)
Password.pack(side = RIGHT)

Lable3 = Label(bottomframe, text="Enter Your Description!   ", font='Source_Sans_Pro 11')
Lable3.pack( side = LEFT)
description = Entry(bottomframe, bd =5)
description.pack(side = RIGHT)

Lable4 = Label(lastframe, text="Enter Your Keyword!       ", font='Source_Sans_Pro 11')
Lable4.pack( side = LEFT)
keyword = Entry(lastframe, bd =5)
keyword.pack(side = RIGHT)

Lable5 = Label(one, text="Enter Your Post Url!         ", font='Source_Sans_Pro 11')
Lable5.pack( side = LEFT)
post_url = Entry(one, bd =5)
post_url.pack(side = RIGHT)

Button_Start = Button(last, text="Start", fg="black", width=50, command=hi)
Button_Start.pack( side = BOTTOM)


pg.FAILSAFE = True


def hi():
    driver = webdriver.Chrome()
    pg.click(1029, 109, duration=.30)
    driver.get('https://www.facebook.com/')
    driver.maximize_window()
    time.sleep(1)

print("Done")
window.mainloop()

错误

Traceback (most recent call last):
  File ".\All.py", line 55, in <module>
    Button_Start = Button(last, text="Start", fg="black", width=50, command=hi)
NameError: name 'hi' is not defined

最佳答案

您在将 hi 函数分配给按钮后定义它。在创建按钮之前放置 hi 函数。

做这样的事情:

def hi():
    ...
    ...

Button_Start = Button(last, text="Start", fg="black", width=50, command=hi)
Button_Start.pack( side = BOTTOM)

关于python - 我的 tkinter 按钮命令功能有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550597/

相关文章:

python - .apply() 函数到数据帧并返回新数据帧?

python - 导入错误 : No module name urllib2

css - 2 三 Angular 形按钮仅重叠 CSS

javascript - 更改onclick事件

python - 动态创建的 kivy 按钮立即运行 on_press 和 on_release

python - 为什么我的 PyObjc Cocoa View 类忘记了它的字段?

c++ - 如何在 C++ 中调用函数时打印每个函数名称?

javascript - 一个 JavaScript 函数运行,但两个函数不运行?

c++ - 相似的程序不同的结果

python - statsmodels ARIMA 结果与原始数据的比较