【发布时间】:2015-11-20 16:42:13
【问题描述】:
我正在寻找一种在按下按钮时启动 while 循环的方法,在本例中是名为“start_loop”的按钮。我非常想知道这是如何完成的,并感谢您在完成此过程中提供的任何帮助,谢谢!
这是完整的 Python 脚本:
from tkinter import *
import win32api
import win32con
from tkinter import messagebox
# defining click as setting the position and starting a click and ending a click
def click(x, y):
win32api.SetCursorPos((x, y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
# Creation of the GUI below
root = Tk()
root.geometry('315x250+250+250') # 315x250 and 250 pixels in x and y direction
root.title("Buffet Time's Auto-Clicker")
# Click
click_label = Label(text='Enter # of clicks here:', fg='green').place(x=30, y=30)
click_entry = Entry().place(x=150, y=30)
# X
x_label = Label(text='Enter the x coordinate here:', fg='black').place(x=30, y=75)
x_entry = Entry().place(x=150, y=75)
# Y
y_label = Label(text='Enter the y coordinate here:', fg='blue').place(x=30, y=120)
y_entry = Entry().place(x=150, y=120)
# Start the loop button
start_loop = Button(text='Press to Start', fg='yellow', bg='black').place(x=110, y=175)
# prompts user before quitting
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
num_of_clicks = click_entry.get()
x_coord = x_entry.get()
y_coord = y_entry.get()
# while loop for the clicking
counter = 0
try:
while counter < num_of_clicks:
click(x_coord, y_coord) # 230, 475 for cookie clicker
if win32api.GetAsyncKeyState(ord('X')):
break
counter += 1
except KeyboardInterrupt:
pass
【问题讨论】:
标签: python user-interface button while-loop tkinter