感谢您阅读本文。我目前正在将Autohokey脚本转换为完整的Python应用程序。此应用程序将帮助我自动化工作中的服务请求。我目前在理解如何将tkinter输入框设置为动态更新的变量时遇到问题。我相信这个代码有一百万个问题,所以试着对我这么简单!这就是我到目前为止所做的:
from tkinter import *
import tkinter as tk
from tkinter.ttk import *
import os
import subprocess as sp
import pyautogui
import pyperclip
class SRHelper:
def __init__(self, master, *args):
self.master = master
self.CustID = tk.StringVar()
self.custLabel = tk.Label(self.master, text="Cust #")
self.custLabel.grid(row=0, column=0, sticky=W)
self.custEnt = tk.Entry(self.master, width=10, textvariable=self.CustID)
self.custEnt.grid(row=0, column=1, sticky=N)
self.artLabel = tk.Label(self.master, text="Articel ID")
self.artLabel.grid(row=0, column=2, sticky=N)
self.artEnt = tk.Entry(self.master, width=12)
self.artEnt.grid(row=0, column=3, sticky=W)
self.srLabel = tk.Label(self.master, text="SR #")
self.srLabel.grid(row=2, column=0, sticky=W, padx=10)
self.srEnt = tk.Entry(self.master, width=10)
self.srEnt.grid(row=2, column=1, sticky=N)
self.aButton = tk.Button(self.master, text="Articles", width=20, command=self.articles_window)
self.aButton.grid(row=2, column=2, columnspan=2, sticky=W)
self.sButton = tk.Button(self.master, text="Start", width=12, height=1)
self.sButton.grid(row=3, column=0, columnspan=2, sticky=W, padx=10)
self.sButton.bind("<Button-1>", self.start_button)
self.dButton = tk.Button(self.master, text="Done", width=12, height=1)
self.dButton.grid(row=4, column=0,columnspan=2, sticky=W, padx=10)
self.bfButton = tk.Button(self.master, text="Filler", width=12, height=1)
self.bfButton.grid(row=5, column=0, columnspan=2, sticky=W, padx=10)
self.taskButton = tk.Button(self.master, text="Task", width=20, height=1)
self.taskButton.grid(row=6, column=2, columnspan=2, sticky=W)
self.userEntry = tk.Entry(self.master, width=12)
self.userEntry.insert(0, "Ctrl+Alt+U")
self.userEntry.config(state='readonly')
self.quickEntry = tk.Entry(self.master, width=12)
self.quickEntry.insert(0, "Ctrl+Alt+Q")
self.quickEntry.config(state='readonly')
self.userEntry.grid(row=7, column=2, columnspan=1, sticky=N, pady=5)
self.quickEntry.grid(row=7, column=3, columnspan=1, sticky=W, pady=5)
#--------- ListBox --------------
self.listbox1 = tk.Listbox(self.master)
self.listbox1.config(width=0, height=6)
for file in os.listdir("Scripts"):
self.listbox1.insert(END, file)
self.listbox1.bind("<Double-Button-1>", double_click)
self.listbox1.bind('<<ListboxSelect>>', single_click)
self.listbox1.grid(row=3, column=2, rowspan= 3, columnspan=2, sticky=W)
def articles_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Articles(self.newWindow)
def start_button(self, *args):
pyautogui.click(219, 174, button='left', clicks=2)
pyautogui.hotkey('ctrl', 'c') # Send, ^c
self.CustID = pyperclip.paste()
print(self.CustID)
def double_click(event):
widget2 = event.widget
selection = widget2.curselection()
# print(selection)
value2 = widget2.get(selection[0])
# print(value)
location = "C:\\SOMELOCATION\\Scripts\\"
openfile = location + value2
# print(openfile)
fullcmd = "notepad " + openfile
sp.Popen(fullcmd, shell=False)
def single_click(event):
widget1 = event.widget
selection = widget1.curselection()
# print(selection)
value1 = widget1.get(selection[0])
# print(value1)
class Articles:
def __init__(self, master):
self.master = master
self.titleLabel = tk.Label(self.master, text="Title", width=12, height=1)
self.titleLabel.grid(row=0, column=0, sticky=W)
self.titleEntry = tk.Entry(self.master, width=40)
self.titleEntry.grid(row=0, column=1, sticky=W)
self.titleLabel = tk.Label(self.master, text="Article #", width=12, height=1)
self.titleLabel.grid(row=0, column=2, sticky=W)
self.artEntry = tk.Entry(self.master, width=15)
self.artEntry.grid(row=0, column=3, columnspan=2, sticky=W)
self.saveButton = tk.Button(self.master, text="Save", width=12, height=1)
self.saveButton.grid(row=0, column=5, sticky=W)
self.searchLabel = tk.Label(self.master, text="Search", width=12, height=1)
self.searchLabel.grid(row=1, column=0, sticky=W)
self.searchEntry = tk.Entry(self.master, width=40)
self.searchEntry.grid(row=1, column=1, sticky=W)
self.openButton = tk.Button(self.master, text="Open", width=12, height=1)
self.openButton.grid(row=1, column=4, sticky=W)
self.clearButton = tk.Button(self.master, text="Clear", width=12, height=1)
self.clearButton.grid(row=1, column=5, sticky=W)
self.tree = ttk.Treeview( self.master, columns=('Title', 'Article #'))
self.tree['show'] = 'headings'
self.tree.heading('#0', text='Articles')
self.tree.heading('#1', text='Title')
self.tree.heading('#2', text='Article #')
self.tree.column('#0', stretch=YES)
self.tree.column('#1', stretch=YES)
self.tree.column('#2', stretch=YES)
self.tree.grid(row=3, column=0, columnspan=6, sticky='nsew')
def close_windows(self):
self.master.destroy()
def main():
root = tk.Tk()
app = SRHelper(root)
root.mainloop()
if __name__ == '__main__':
main()
问题在于start_button函数。当按下“开始”按钮时,我希望它将鼠标移动到屏幕上的特定位置,将其中的内容复制到关联变量中,并将其自动更新到主GUI窗口中的不同输入框中。我有一种感觉问题是类和方法/函数之间的继承。我还在学习,所以任何解释都会非常感激。谢谢!
答案 0 :(得分:2)
你非常接近。只是tkinter变量不像python变量。您必须使用set
方法更改值:
self.CustID.set(pyperclip.paste())
检索值的get()
方法:
print(self.CustID.get())