Python3和Tkinter,我无法通过单选按钮更改变量的值。

时间:2018-08-05 20:15:42

标签: python-3.x tkinter

试图编写一个程序来使用键盘移动鼠标并在屏幕上的gui按钮上记录位置。我正在使用名为speed的变量来更快地在屏幕上移动鼠标。选择不同的单选按钮时,为什么无法更改变速?谢谢您的帮助

import pyautogui, readchar
from tkinter.ttk import *
import tkinter as tk
import time

#rootsetup  
root=tk.Tk()
root.title("GUI Mapping Tool")
root.geometry('650x200')
CurrentPostionX, CurrentPostionY=  pyautogui.position()

#Circle or Rectangle radio
Circle = Radiobutton(root,text='Circle', value=101)
Rectangle = Radiobutton(root,text='Rectangle', value=102)
Circle.grid(column=4, row=1)
Rectangle.grid(column=3, row=1)
#Speed radio
speedchoice=tk.IntVar()       
def selected():

  if speedchoice.get()==1:
        Speed=1
        print("1")
  if speedchoice.get()==10:
        Speed=10
        print("10")
  if speedchoice.get()==25:
        Speed=25
        print("25")
  if speedchoice.get()==100:
        Speed=100
        print("100")
Speed1 = Radiobutton(root,text='1x', value=1 , command=selected, variable= speedchoice) 

Speed10 = Radiobutton(root,text='10x', value=10,  command=selected, variable= speedchoice )

Speed25 = Radiobutton(root, text='25x', value=25,  command=selected, variable= speedchoice )

Speed100 = Radiobutton(root,text='100x', value=100, command=selected, variable= speedchoice )

Speed1.grid(column=4, row=0)

Speed10.grid(column=5, row=0)

Speed25.grid(column=6, row=0)

Speed100.grid(column=7, row=0)


#MoveMouseWithArrows:
def key(event):

    CurrentPostionX, CurrentPostionY=  pyautogui.position()


    Direction=event.keysym
     if Direction=='Up':
        pyautogui.moveTo(CurrentPostionX, CurrentPostionY-1*Speed)      
     if event.keysym =='Down':
        pyautogui.moveTo(CurrentPostionX, CurrentPostionY+1*Speed)
     if event.keysym =='Left':
        pyautogui.moveTo(CurrentPostionX-1*Speed, CurrentPostionY)
     if event.keysym =='Right':
        pyautogui.moveTo(CurrentPostionX+1*Speed, CurrentPostionY)
root.bind_all('<Key>', key)
# Locations Name Entry
txt = Entry(root,width=20)
txt.grid(column=3, row=0)
CurrentPostionX, CurrentPostionY=  pyautogui.position()
Xpos=tk.StringVar()
Ypos=tk.StringVar()
def update_label():

   while 1>0:
      CurrentPostionX, CurrentPostionY=  pyautogui.position()
      Xpos.set(str(CurrentPostionX))
      Ypos.set(str(CurrentPostionY))
      root.update()
Xaxislabel=tk.Label(root, text= "X:", font=("Arial Bold", 50))
Xaxislabel.grid(column=0, row=0)
Xposlabel=tk.Label(root,textvariable=Xpos, font=("Arial Bold", 50))
Xposlabel.grid(column=1, row=0)
Yaxislabel=tk.Label(root, text= "Y:", font=("Arial Bold", 50))
Yaxislabel.grid(column=0, row=1)
Yposlabel=tk.Label(root,textvariable=Ypos, font=("Arial Bold", 50))
Yposlabel.grid(column=1, row=1)
update_label()
root.mainloop()

0 个答案:

没有答案