python abaqus 和 tkinter GUI 脚本

python script for abaqus and tkinter GUI

你好堆栈溢出社区, 我正在为 abaqus 编写 python 脚本。我必须使用它们的路径将零件导入 abaqus,当零件更改时路径必须更改,因此我使用 tkinter 创建了一个 GUI 以在必要时放置零件的路径。 这是我的问题:如何读取从 GUI 到源代码的路径(变量)?

请原谅我的英语不好

这是 GUI 的代码

import tkinter

class simpleapp_tk(tkinter.Tk):
    def __init__(self,parent):
        tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable1 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable1)
        self.entry.grid(column=0,row=1,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable1.set(u"Enter text here.")

        self.entryVariable2 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable2)
        self.entry.grid(column=0,row=2,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable2.set(u"Enter text here.")

        self.entryVariable3 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable3)
        self.entry.grid(column=0,row=3,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable3.set(u"Enter text here.")

        self.entryVariable4 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable4)
        self.entry.grid(column=0,row=4,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable4.set(u"Enter text here.")

        self.entryVariable5 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable5)
        self.entry.grid(column=0,row=5,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable5.set(u"Enter text here.")

        self.entryVariable6 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable6)
        self.entry.grid(column=0,row=6,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable6.set(u"Enter text here.")

        self.entryVariable7 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable7)
        self.entry.grid(column=0,row=7,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable7.set(u"Enter text here.")

        self.entryVariable8 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable8)
        self.entry.grid(column=0,row=8,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable8.set(u"Enter text here.")

        self.entryVariable9 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable9)
        self.entry.grid(column=0,row=9,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable9.set(u"Enter text here.")

        self.entryVariable10 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable10)
        self.entry.grid(column=0,row=10,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable10.set(u"Enter text here.")

        Label = tkinter.Label(self,text=u"  chemin table de paramétrage  ")
        Label.grid(column=2,row=1)

        Label = tkinter.Label(self,text=u"  chemin axe  ")
        Label.grid(column=2,row=2)

        Label = tkinter.Label(self,text=u"  chemin bague exterieure  ")
        Label.grid(column=2,row=3)

        Label = tkinter.Label(self,text=u"  chemin bague interieure  ")
        Label.grid(column=2,row=4)

        Label = tkinter.Label(self,text=u"  chemin bague  ")
        Label.grid(column=2,row=5)

        Label = tkinter.Label(self,text=u"  chemin calle  ")
        Label.grid(column=2,row=6)

        Label = tkinter.Label(self,text=u"  chemin lame M2  ")
        Label.grid(column=2,row=7)

        Label = tkinter.Label(self,text=u"  chemin lame M1  ")
        Label.grid(column=2,row=8)

        Label = tkinter.Label(self,text=u"  chemin Patin1  ")
        Label.grid(column=2,row=9)

        Label = tkinter.Label(self,text=u"  chemin Patin2  ")
        Label.grid(column=2,row=10)

        button = tkinter.Button(self,text=u"  DONE  ")
        button.grid(column=1,row=11)
        self.labelVariable = tkinter.StringVar()
        label = tkinter.Label(self,textvariable=self.labelVariable,
                              anchor="w",fg="white",bg="black")
        label.grid(column=0,row=0,columnspan=3,sticky='EW')
        self.labelVariable.set(u"hello have a nice day")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('GUI for script abaqus')
    app.mainloop()

这是我想在我的源代码中进行更改的地方

part1Directory='C:/Users/khalil/Desktop/PFE/pieces/axe.stp'
part2Directory='C:/Users/khalil/Desktop/PFE/pieces/bague exterieur.stp'
part3Directory='C:/Users/khalil/Desktop/PFE/pieces/bague interieure.stp'
part4Directory='C:/Users/khalil/Desktop/PFE/pieces/bague.stp'
part5Directory='C:/Users/khalil/Desktop/PFE/pieces/calle.stp'
part6Directory='C:/Users/khalil/Desktop/PFE/pieces/lame inf.stp'
part7Directory='C:/Users/khalil/Desktop/PFE/pieces/lame sup.stp'
part8Directory='C:/Users/khalil/Desktop/PFE/pieces/patin 2.stp'
part9Directory='C:/Users/khalil/Desktop/PFE/pieces/patin.stp'

您发布的代码的简化和精简版本。这不是一个完整的解决方案,但应该让您了解它是如何完成的。

from functools import partial
import tkinter

class simpleapp_tk():
    def __init__(self,parent):
        self.parent = parent
        self.initialize()

    def initialize(self):
##        self.grid()
        self.entry_variables=[]
        for ctr in range(9):
            entryVariable = tkinter.StringVar()
            ## don't have to keep 10 instances of Entry class crap
            ## because you are using a StringVar()
            entry=tkinter.Entry(textvariable=entryVariable, width=75)
            entry.grid(column=0,row=ctr+1,sticky='EW')
            entry.bind("<Return>", partial(self.OnPressEnter, ctr))
            entryVariable.set(u"Enter text here.")
            self.entry_variables.append(entryVariable)

        tkinter.Button(text=u"  DONE  ", bg="lightblue",
                 command=self.parent.quit).grid(column=0,row=11)

        """
        self.entryVariable2 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable2)
        self.entry.grid(column=0,row=2,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable2.set(u"Enter text here.")

        self.entryVariable3 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable3)
        self.entry.grid(column=0,row=3,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable3.set(u"Enter text here.")

        self.entryVariable4 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable4)
        self.entry.grid(column=0,row=4,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable4.set(u"Enter text here.")

        self.entryVariable5 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable5)
        self.entry.grid(column=0,row=5,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable5.set(u"Enter text here.")

        self.entryVariable6 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable6)
        self.entry.grid(column=0,row=6,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable6.set(u"Enter text here.")

        self.entryVariable7 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable7)
        self.entry.grid(column=0,row=7,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable7.set(u"Enter text here.")

        self.entryVariable8 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable8)
        self.entry.grid(column=0,row=8,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable8.set(u"Enter text here.")

        self.entryVariable9 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable9)
        self.entry.grid(column=0,row=9,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable9.set(u"Enter text here.")

        self.entryVariable10 = tkinter.StringVar()
        self.entry = tkinter.Entry(self,textvariable=self.entryVariable10)
        self.entry.grid(column=0,row=10,sticky='EW')
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable10.set(u"Enter text here.")

        Label = tkinter.Label(self,text=u"  chemin table de parametrage  ")
        Label.grid(column=2,row=1)

        Label = tkinter.Label(self,text=u"  chemin axe  ")
        Label.grid(column=2,row=2)

        Label = tkinter.Label(self,text=u"  chemin bague exterieure  ")
        Label.grid(column=2,row=3)

        Label = tkinter.Label(self,text=u"  chemin bague interieure  ")
        Label.grid(column=2,row=4)

        Label = tkinter.Label(self,text=u"  chemin bague  ")
        Label.grid(column=2,row=5)

        Label = tkinter.Label(self,text=u"  chemin calle  ")
        Label.grid(column=2,row=6)

        Label = tkinter.Label(self,text=u"  chemin lame M2  ")
        Label.grid(column=2,row=7)

        Label = tkinter.Label(self,text=u"  chemin lame M1  ")
        Label.grid(column=2,row=8)

        Label = tkinter.Label(self,text=u"  chemin Patin1  ")
        Label.grid(column=2,row=9)

        Label = tkinter.Label(self,text=u"  chemin Patin2  ")
        Label.grid(column=2,row=10)

        self.labelVariable = tkinter.StringVar()
        label = tkinter.Label(self,textvariable=self.labelVariable,
                              anchor="w",fg="white",bg="black")
        label.grid(column=0,row=0,columnspan=3,sticky='EW')
        self.labelVariable.set(u"hello have a nice day")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)
        """

    def OnPressEnter(self, list_location, event=None):
        dirs_list=['C:/Users/khalil/Desktop/PFE/pieces/axe.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/bague exterieur.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/bague interieure.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/bague.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/calle.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/lame inf.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/lame sup.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/patin 2.stp',
                   'C:/Users/khalil/Desktop/PFE/pieces/patin.stp']
        text_entered=self.entry_variables[list_location].get()
        self.entry_variables[list_location].set("%s-->%s" % (
                             text_entered, dirs_list[list_location]))
##        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
##        self.entry.focus_set()
##        self.entry.selection_range(0, tkinter.END)

if __name__ == "__main__":
    top=tkinter.Tk()
    app = simpleapp_tk(top)
    top.mainloop()