从Tkinter中的另一个类调用函数

时间:2014-11-19 20:14:54

标签: python canvas tkinter

我在一个单独的python脚本中有一个类,我希望使用Tkinter链接到画布中的外部按钮。当前类现在有框架,标签和按钮,我可以在单击外部按钮时弹出它,但是当我在root = Tk()之前将frame放在data.py中时,没有任何功能可以正常工作脚本。

import Tkinter as ttk
from Tkinter import *
import tkMessageBox
import data


def XYevent():
    data.makeXY()


top = ttk.Tk()
C = ttk.Canvas(top, bg="white", height=250, width=300)
C.pack()
B=ttk.Button(C, text="Create XY Feature", command=XYevent)
B.pack()

top.mainloop()

这是我的脚本到目前为止,但单击按钮后没有任何功能在弹出窗口中工作。我在画布脚本中错过了一个命令吗?单独运行时的数据。

这是带有我想要链接到画布窗口的命令的data.py脚本,最终我想要两个按钮打开一个与第一个但不同用户选项相关的不同窗口。

import arcpy
import Tkinter as ttk
import tkFileDialog
from arcpy import env
from Tkinter import *
import tkMessageBox


class makeXY():
    def __init__(self):
        self.main()

    def XY(self,*args):
        arcpy.MakeXYEventLayer_management(self.dbtable.get(), self.easting.get(), self.northing.get(),
                                       "Layers", self.prj.get())
        arcpy.CopyFeatures_management("Layers", self.New_Shapefile.get())
        tkMessageBox.showinfo("Create ArcGIS feature", arcpy.GetMessages())

    def main(self):
        frame = ttk.Frame(root, padx=3, relief=SUNKEN, borderwidth=3)
        frame.grid(column=0, row=0, sticky=(N, W, E, S))
        frame.columnconfigure(0, weight=1)
        frame.rowconfigure(0, weight=1)

        self.labelVariable = IntVar()
        XYtitle = ttk.Label(frame, textvariable=self.labelVariable,
                        relief=GROOVE, fg="white", bg="#2C5E17", font=("Georgia", 12))
        XYtitle.grid(column=0, row=0, columnspan=5, sticky='EW', pady=5)
        self.labelVariable.set("Create A Feature For ArcGIS Using XY Data")


        # Creates an XY event
        self.dbtable = StringVar()
        self.dbtable_entry = ttk.Entry(frame, width=50, textvariable=self.dbtable).grid(column=1, row=2, sticky='WE')
        ttk.Button(frame, text="Input Table",command=lambda:self.dbtable.set
               (tkFileDialog.askopenfilename(filetypes=[("All files",'*.*')]))).grid(row=2, column=2, sticky='WE')
        self.easting = StringVar()
        self.easting_entry = ttk.Entry(frame, width=20, textvariable=self.easting).grid(column=1, row=3, sticky=W)
        self.northing = StringVar()
        self.northing_entry = ttk.Entry(frame, width=20, textvariable=self.northing).grid(column=1, row=4, sticky=W)
        self.prj = StringVar()
        ttk.Button(frame, text="Projection File",command=lambda:self.prj.set
               (tkFileDialog.askopenfilename(filetypes=[("Project File",'.prj')]))).grid(row=5, column=2, sticky='WE')
        ttk.Entry(frame, width=50, textvariable=self.prj).grid(column=1, row=5, sticky='WE')
        self.New_Shapefile = StringVar()
        ttk.Entry(frame, width=50, textvariable=self.New_Shapefile).grid(column=1, row=6, sticky='WE')
        ttk.Button(frame, text="Output Feature",
                    command=lambda:self.New_Shapefile.set
               (tkFileDialog.asksaveasfilename(filetypes=[("Feature Datasets", {'.shp','.gdb','.mdb'})]))).grid(row=6, column=2, sticky='WE')

       ttk.Button(frame, text="Create Feature", command=self.XY).grid(column=1, row=7, sticky=W)
       ttk.Label(frame, text="Input table with XY data: ").grid(column=0,row=2, sticky=W)
       ttk.Label(frame, text="Enter X (easting) field name: ").grid(column=0, row=3, sticky=W)
       ttk.Label(frame, text="Enter Y (northing) field name: ").grid(column=0, row=4, sticky=W)
       ttk.Label(frame, text="Enter projection file: ").grid(column=0, row=5, sticky=W)
       ttk.Label(frame, text="Feature output (shapefile, geodatabase):").grid(column=0, row=6, sticky=W)

       root.bind('<Return>', self.XY)

if __name__== "__main__":
    root = Tk()
    root.option_add("*Background", "#EBF1E8")
    root.option_add("*Entry.Background", "white")
    root.option_add("*Button.Background", "#afc8a4")
    root.option_add("*Label.Font", "helvetica 10")
    root.option_add("*Button.Font", "helvetica 9")
    root.title("XY Data to ArcGIS")
    client = makeXY()
    root.mainloop()

2 个答案:

答案 0 :(得分:1)

我们不知道XY_Tk包含什么,所以不知道它应该做什么我只是在Adding or deleting tkinter widgets from within other modules回答了类似的问题,这可能有所帮助。

答案 1 :(得分:0)

我通过取出

来解决这个问题
root.bind() 

if __name__ == "__main__" 

来自班级并使用

ttk.mainloop() 

在调用该类的脚本中。

这是我的工作主脚本,其中的按钮调用两个类XYdata.py和selectfeature.py:

import Tkinter as ttk
import tkFileDialog
from Tkinter import *
import tkMessageBox
import XYdata
from XYdata import *
import selectfeature
from selectfeature import *


def XYevent():
    XY.main()

def select():
    s.main()

root = ttk.Tk()
root.geometry("650x275+500+100")
root.option_add("*Background", "#EBF1E8")
root.option_add("*Entry.Background", "white")
root.option_add("*Button.Background", "#afc8a4")
root.option_add("*Label.Font", "helvetica 10")
root.option_add("*Button.Font", "helvetica 9")
root.title("Create Point Features for ArcGIS")

myframe = ttk.Frame(root,relief="groove",bd=1)
myframe.place(x=7, y=5, width=635, height=175)

canvas = ttk.Canvas(myframe)
frame = ttk.Frame(canvas)
canvas.pack(side="left", fill=BOTH)


canvaslbl= Label(canvas,text=' Create features for ArcGIS using two button options'),
             font=("Georgia", 12), justify=LEFT)
canvaslbl.pack()
canvas.create_window((0,0), window=frame, anchor='nw')
frame.bind("<Return>", XYevent)

b1 = ttk.Button(root, command=XYevent, text="XY Feature Class",fg="white",bg="#39442C", font=("helvetica", 10)) 
b1.place(x = 125, y = 210, width=180, height=40)
b2 = ttk.Button(root, command=select, text="Selection Feature Class",fg="white", bg="#39442C", font=("helvetica", 10))
b2.place(x = 350, y = 210, width=180, height=40)

XY = XYdata.makeXY(ttk, root)
s = selectfeature.selection(ttk, root)
ttk.mainloop()