【问题标题】:confirm and mainloop in pyautogui, and tkinterpyautogui 和 tkinter 中的确认和主循环
【发布时间】:2020-10-22 13:54:05
【问题描述】:

我正在寻找可以在 Tkinter 小部件中使用 x 值的方法。 我无法从网上找到合适的答案。 任何建议表示赞赏。 谢谢


关于,确认,它返回 x 并且 print(x) 有效。

from pyautogui import *    
def test():
    global x
    x = confirm(buttons=['z2037', 'z2039'])        
test()
print(x)

from pyautogui import *
from tkinter import *
root = Tk()    
def test():
    global x
    x = confirm(buttons=['Z2 037', 'Z2 039'])    

b = Button(root, text='KLO', padx=50, pady=50, command=test)
b.pack()

root.mainloop()
print(x)

如果我在 root.mainloop() 之上添加 print(x),Python 会抛出一个错误,说名称“x”虽然是全局的,但没有定义。 如果我在 root.mainloop() 下添加 print(x),它会起作用。

但是,我想在 tkinter 小部件中使用 x 值(Z2 037 或 Z2 039)。有什么办法吗?

【问题讨论】:

    标签: python tkinter pyautogui confirm mainloop


    【解决方案1】:

    好吧,根据我收集到的信息,您希望每次按下其中一个按钮时都打印出来(我可能是错的)

    这是我设法得到的

    from pyautogui import *
    from tkinter import *
    root = Tk()
    root.geometry('700x600+500+100')
    
    
    def test():
        x = confirm(buttons=['Z2 037', 'Z2 039'])
    
        print(x)
    
    b = Button(root, text='KLO', padx=50, pady=50, command=test)
    b.pack()
    
    
    root.mainloop()
    

    输出:

    Z2 039
    Z2 037
    

    【讨论】:

    • 谢谢@Alasdair,但我想使用 x 值作为全局(函数外),即 Z2 037 或 Z2 039
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多