我编写的脚本允许我使用tkinter条目小部件输入值,该小部件将用于设置函数生成器。
每当我尝试使用tkinter而不仅仅是python input()函数时,它会给我这个错误:
lowf = int(float(inputlowf) * 10)
ValueError: could not convert string to float:
以下是我所拥有的:(使用pywin32来控制函数发生器)
import win32api, win32con
import time
import win32com.client
import tkinter
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)
shell = win32com.client.Dispatch("WScript.Shell")
master = tkinter.Tk()
tkinter.Label(master, text="Starting Frequency").grid(row=0)
tkinter.Label(master, text="Ending Frequency").grid(row=1)
tkinter.Label(master, text="Increment").grid(row=2)
line1 = tkinter.Entry(master)
line2 = tkinter.Entry(master)
line3 = tkinter.Entry(master)
line1.grid(row=0, column=1)
line2.grid(row=1, column=1)
line3.grid(row=2, column=1)
inputlowf = line1.get()
inputhighf = line2.get()
inputstep = line3.get()
tkinter.Button(master, text='OK', command=master.destroy).grid(row=3, column=1, sticky=tkinter.W, pady=4)
tkinter.mainloop()
lowf = int(float(inputlowf) * 10)
highf = int(float(inputhighf) * 10)
step = int(float(inputstep) * 10)
for z in range(lowf,(highf + step),step):
file = z
freq = z / 10
print(freq)
time.sleep(.5)
答案 0 :(得分:4)
inputlowf = line1.get()
inputhighf = line2.get()
inputstep = line3.get()
这些3行在您运行脚本后立即执行,因此inputlowf
,inputhighf
和& inputstep
变量包含初始化Entry
小部件时存在的空条目字段。尝试制作一个回调函数,只要单击“确定”即可运行,该函数将获取值。
试试看看我的意思:
import win32api, win32con
import time
import win32com.client
import Tkinter
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)
shell = win32com.client.Dispatch("WScript.Shell")
master = Tkinter.Tk()
Tkinter.Label(master, text="Starting Frequency").grid(row=0)
Tkinter.Label(master, text="Ending Frequency").grid(row=1)
Tkinter.Label(master, text="Increment").grid(row=2)
line1 = Tkinter.Entry(master)
line2 = Tkinter.Entry(master)
line3 = Tkinter.Entry(master)
line1.grid(row=0, column=1)
line2.grid(row=1, column=1)
line3.grid(row=2, column=1)
def callback():
global line1, line2, line3, master, lowf, highf, step
inputlowf = line1.get()
inputhighf = line2.get()
inputstep = line3.get()
try:
lowf = int(float(inputlowf) * 10)
highf = int(float(inputhighf) * 10)
step = int(float(inputstep) * 10)
print lowf
print highf
print step
master.destroy()
except ValueError:
print "Error with input"
myButton = Tkinter.Button(master, text='OK', command=callback).grid(row=3, column=1, sticky=Tkinter.W, pady=4)
Tkinter.mainloop()