Python:导入的Tkinter程序自己运行?

时间:2015-02-03 00:47:54

标签: python import tkinter window runtime

所以我正在为我的最后一年项目写一个程序,特别是这件事一直困扰着我。我已经这么久了,我无法理解,所以我希望有人能够对它有所了解!

基本上,我的课程是为我学校的运动日录制和存储结果。这个想法是学校将来会实际使用它,所以我希望它既美观又实用。当然,这意味着要集成某种UI,因为我之前使用过Tkinter似乎是一种显而易见的方法。

到目前为止,我刚刚编写了基本窗口。我有两个窗口:一个主窗口和一个用于更改参与者名称的“编辑”窗口。我已经在单独的 .py 文件中编写了这两个窗口的代码,并将“ edit ”窗口文件导入到主文件中,以便我可以调用它在主窗口中单击“ EDIT ”按钮时运行。但是,无论出于何种原因,“编辑”窗口只是在我运行主程序时自行运行,即使它的唯一引用是import editWindow

这是主窗口的代码:

'''
Created on 19 Jan 2015

@author: James.D.Wood
'''

#!/usr/bin/env python

#init
from tkinter import *
import editWindow

global event, year, cr, sr, a1, a2, s1, s2, d1, d2, n1, n2, ar1, ar2, sr1, sr2, dr1, dr2, nr1, nr2, msg, b, s, g

events = [
          "70m hurdles",
          "75m hurdles",
          "80m hurdles",
          "100m hurdles",          
          "100m",
          "200m",
          "300m",
          "400m",
          "800m",
          "1500m",
          "Javelin",
          "Long-jump",
          "High-jump",
          "Triple-jump",
          "Shot-put",
          "Discus",
          "Relay"
          ]
years = [
         "7",
         "8",
         "9",
         "10",
         "11",
         "12",
         "13"
         ]


#create/define window
    #main win
win = Tk()
win.title("Queens' Athletics")

win.minsize(800,600)
win.maxsize(800,600)


    #section 1
event = StringVar() #event var
event.set("Select event...") #default
year = StringVar() #year var
year.set("Select year...") #default
lineImg = PhotoImage(file="resources/line.gif", master=win) #load separator image

sel = Label(win, text="Select event:")
slctEvnt = OptionMenu(win, event, *events) #dropdown box
slctEvnt.config(width=20)
selYr = Label(win, text="Select year:")
slctYr = OptionMenu(win, year, *years) #dropdown box
slctYr.config(width=20)
line = Label(win, image=lineImg) #separator
line.image = lineImg

#pack into window
sel.grid(row=0, column=0, sticky=W)
slctEvnt.grid(row=0, column=1, columnspan=4, sticky=W)
selYr.grid(row=1, column=0, sticky=W)
slctYr.grid(row=1, column=1, columnspan=4, sticky=W)
line.grid(row=2, column=0, columnspan=4, sticky=W, pady=15)


    #section 2
cr = StringVar() #county record var
cr.set(" - ") #default
sr = StringVar() #school record var
sr.set(" - ") #default

crl = Label(win, text="County record:")
crBox = Label(win, width=6, textvariable=cr, state='normal', relief='sunk', bg="grey") #county record box
srl = Label(win, text="School record:")
srBox = Label(win, width=6, textvariable=sr, state='normal', relief='sunk', bg="grey") #school record box

#pack into window
crl.grid(row=3, column=0, sticky=W)
crBox.grid(row=3, column=1, sticky=W, padx=10)
srl.grid(row=3, column=2, sticky=W)
srBox.grid(row=3, column=3, sticky=W, padx=10)


    #section 3
#participant names
a1 = StringVar()
a1.set(". . .") #default
a2 = StringVar()
a2.set(". . .") #default
s1 = StringVar()
s1.set(". . .") #default
s2 = StringVar()
s2.set(". . .") #default
d1 = StringVar()
d1.set(". . .") #default
d2 = StringVar()
d2.set(". . .") #default
n1 = StringVar()
n1.set(". . .") #default
n2 = StringVar()
n2.set(". . .") #default
#results
ar1 = StringVar()
ar2 = StringVar()
sr1 = StringVar()
sr2 = StringVar()
dr1 = StringVar()
dr2 = StringVar()
nr1 = StringVar()
nr2 = StringVar()
#
vLineImg = PhotoImage(file="resources/vline.gif", master=win) #load separator image

blank1 = Label(win, text="") #blank separator
part = Label(win, text="Students participating:")
resul = Label(win, text="      Results")
#participant names
auden1 = Label(win, width=36, textvariable=a1, state='normal', relief='sunk', bg="yellow")
auden2 = Label(win, width=36, textvariable=a2, state='normal', relief='sunk', bg="yellow")
suthe1 = Label(win, width=36, textvariable=s1, state='normal', relief='sunk', bg="blue")
suthe2 = Label(win, width=36, textvariable=s2, state='normal', relief='sunk', bg="blue")
drake1 = Label(win, width=36, textvariable=d1, state='normal', relief='sunk', bg="green")
drake2 = Label(win, width=36, textvariable=d2, state='normal', relief='sunk', bg="green")
newto1 = Label(win, width=36, textvariable=n1, state='normal', relief='sunk', bg="red")
newto2 = Label(win, width=36, textvariable=n2, state='normal', relief='sunk', bg="red")
#results
auRes1 = Entry(win, width=12, textvariable=ar1, justify=CENTER, state='normal', relief='sunk', bg="yellow")
auRes2 = Entry(win, width=12, textvariable=ar2, justify=CENTER, state='normal', relief='sunk', bg="yellow")
suRes1 = Entry(win, width=12, textvariable=sr1, justify=CENTER, state='normal', relief='sunk', bg="blue")
suRes2 = Entry(win, width=12, textvariable=sr2, justify=CENTER, state='normal', relief='sunk', bg="blue")
drRes1 = Entry(win, width=12, textvariable=dr1, justify=CENTER, state='normal', relief='sunk', bg="green")
drRes2 = Entry(win, width=12, textvariable=dr2, justify=CENTER, state='normal', relief='sunk', bg="green")
neRes1 = Entry(win, width=12, textvariable=nr1, justify=CENTER, state='normal', relief='sunk', bg="red")
neRes2 = Entry(win, width=12, textvariable=nr2, justify=CENTER, state='normal', relief='sunk', bg="red")
#
vLine = Label(win, image=vLineImg)
vLine.image = vLineImg

#pack into window
blank1.grid(row=5)
part.grid(row=6, column=0, columnspan=2, sticky=W, pady=8)
resul.grid(row=7, column=3, sticky=W)
#participant names
auden1.grid(row=8, column=0, columnspan=3, sticky=W, padx=8)
auden2.grid(row=9, column=0, columnspan=3, sticky=W, padx=8)
suthe1.grid(row=10, column=0, columnspan=3, sticky=W, padx=8)
suthe2.grid(row=11, column=0, columnspan=3, sticky=W, padx=8)
drake1.grid(row=12, column=0, columnspan=3, sticky=W, padx=8)
drake2.grid(row=13, column=0, columnspan=3, sticky=W, padx=8)
newto1.grid(row=14, column=0, columnspan=3, sticky=W, padx=8)
newto2.grid(row=15, column=0, columnspan=3, sticky=W, padx=8)
#results
auRes1.grid(row=8, column=3, columnspan=2, sticky=W)
auRes2.grid(row=9, column=3, columnspan=2, sticky=W)
suRes1.grid(row=10, column=3, columnspan=2, sticky=W)
suRes2.grid(row=11, column=3, columnspan=2, sticky=W)
drRes1.grid(row=12, column=3, columnspan=2, sticky=W)
drRes2.grid(row=13, column=3, columnspan=2, sticky=W)
neRes1.grid(row=14, column=3, columnspan=2, sticky=W)
neRes2.grid(row=15, column=3, columnspan=2, sticky=W)
#
vLine.grid(row=8, column=4, rowspan=8, sticky=W, padx=35)


    #section 4
msg = StringVar()
msg.set("[                ]")

edit = Button(win, width=8, text="Edit >", padx=15, pady=8)
submit = Button(win, width=8, text="Submit >", padx=15, pady=8)
msgBox = Message(win, textvariable=msg, anchor=CENTER, justify=CENTER, aspect=200, borderwidth=2, relief='ridge', bg='grey')
reset = Button(win, width=8, text="Reset >", padx=15, pady=8)

#pack into window
edit.grid(row=16, column=1, rowspan=2, sticky=S, pady=12)
submit.grid(row=16, column=3, rowspan=2, sticky=S, pady=12)
msgBox.grid(row=16, column=4, rowspan=2, columnspan=3, sticky=S, pady=16)
reset.grid(row=16, column=7, rowspan=2, sticky=S, pady=12)


    #section 5
img = PhotoImage(file="resources/logo.gif", master=win)

logo = Label(win, image=img)
logo.image = img

logo.grid(row=0, column=4, rowspan=7, columnspan=4, sticky=W+E+N+S, padx=60, pady=5)


    #section 6
#leading participants
b = StringVar()
b.set(". . .") #default
s = StringVar()
s.set(". . .") #default
g = StringVar()
g.set(". . .") #default

stand = Label(win, text="Current standing:")
first = Label(win, text="1st")
secon = Label(win, text="2nd")
third = Label(win, text="3rd")
#leading participants
bron = Label(win, width=20, height=2, textvariable=b, state='normal', relief='sunk', bg="brown")
silv = Label(win, width=20, height=2, textvariable=s, state='normal', relief='sunk', bg="lightgrey")
gold = Label(win, width=20, height=2, textvariable=g, state='normal', relief='sunk', bg="gold")

#pack into window
stand.grid(row=7, column=5, rowspan=2, columnspan=3, sticky=E, padx=50)
first.grid(row=9, column=4, rowspan=2, sticky=E, padx=2)
secon.grid(row=11, column=4, rowspan=2, sticky=E, padx=2)
third.grid(row=13, column=4, rowspan=2, sticky=E, padx=2)
#leading participants
bron.grid(row=9, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)
silv.grid(row=11, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)
gold.grid(row=13, column=5, rowspan=2, columnspan=3, sticky=E, padx=22)


win.mainloop()

...以及“编辑”窗口的代码:

'''
Created on 29 Jan 2015

@author: James.D.Wood
'''

#!/usr/bin/env python

#init
from tkinter import *

global whichEvent, ea1, ea2, es1, es2, ed1, ed2, en1, en2

#create/define window
    #edit window
Tk().withdraw()
editWin = Toplevel()
editWin.title("Edit participants")

editWin.minsize(350,400)
editWin.maxsize(350,400)


#define elements
whichEvent = StringVar()
whichEvent.set("[EVENT]") #testing
editLineImg = PhotoImage(file="resources/sline.gif", master=editWin)
#edit names boxes
ea1 = StringVar()
ea2 = StringVar()
es1 = StringVar()
es2 = StringVar()
ed1 = StringVar()
ed2 = StringVar()
en1 = StringVar()
en2 = StringVar()

editing = Label(editWin, text="Editing for:")
editEvent = Label(editWin, width=30, textvariable=whichEvent, state='normal', relief='sunk', bg='grey')
editLine = Label(editWin, image=editLineImg) #separator
editLine.image = editLineImg
blank2 = Label(editWin, text="")
blank3 = Label(editWin, text="")
blank4 = Label(editWin, text="")
blank5 = Label(editWin, text="")
#house labels
labAud = Label(editWin, text="Auden:")
labSut = Label(editWin, text="Sutherland:")
labDra = Label(editWin, text="Drake:")
labNew = Label(editWin, text="Newton:")
#edit names boxes
editAud1 = Entry(editWin, width=30, textvariable=ea1, justify=CENTER, state='normal', relief='sunk', bg='yellow')
editAud2 = Entry(editWin, width=30, textvariable=ea2, justify=CENTER, state='normal', relief='sunk', bg='yellow')
editSut1 = Entry(editWin, width=30, textvariable=es1, justify=CENTER, state='normal', relief='sunk', bg='blue')
editSut2 = Entry(editWin, width=30, textvariable=es2, justify=CENTER, state='normal', relief='sunk', bg='blue')
editDra1 = Entry(editWin, width=30, textvariable=ed1, justify=CENTER, state='normal', relief='sunk', bg='green')
editDra2 = Entry(editWin, width=30, textvariable=ed2, justify=CENTER, state='normal', relief='sunk', bg='green')
editNew1 = Entry(editWin, width=30, textvariable=en1, justify=CENTER, state='normal', relief='sunk', bg='red')
editNew2 = Entry(editWin, width=30, textvariable=en2, justify=CENTER, state='normal', relief='sunk', bg='red')

#pack into window
editing.grid(row=0, column=0, sticky=W, pady=15)
editEvent.grid(row=0, column=1, sticky=W, pady=10)
editLine.grid(row=1, column=0, columnspan=3, sticky=W)
#house labels
blank2.grid(row=2, column=1, sticky=W)
labAud.grid(row=3, column=0, rowspan=2, sticky=W, pady=10)
blank3.grid(row=5, column=1, sticky=W)
labSut.grid(row=6, column=0, rowspan=2, sticky=W, pady=10)
blank4.grid(row=8, column=1, sticky=W)
labDra.grid(row=9, column=0, rowspan=2, sticky=W, pady=10)
blank5.grid(row=11, column=1, sticky=W)
labNew.grid(row=12, column=0, rowspan=2, sticky=W, pady=10)
#edit names boxes
editAud1.grid(row=3, column=1, sticky=W)
editAud2.grid(row=4, column=1, sticky=W)
editSut1.grid(row=6, column=1, sticky=W)
editSut2.grid(row=7, column=1, sticky=W)
editDra1.grid(row=9, column=1, sticky=W)
editDra2.grid(row=10, column=1, sticky=W)
editNew1.grid(row=12, column=1, sticky=W)
editNew2.grid(row=13, column=1, sticky=W)

我现在正处于这样的地步,我想知道在一个 .py 文件中编写整个程序是否真的更好,尽管我认为这样做会更优雅。

如果有人可以建议一个很好的,简化的解决方案来保持两个窗口的代码分开,我将非常感激。我正在使用Python 3.4,FYI。

感谢。

2 个答案:

答案 0 :(得分:5)

导入模块时,您正在执行其代码。那个导入模块实际上如何在python中工作。要停止它,请将代码放入函数或类中的导入模块中。公平地说,它仍然在运行以制作函数或类,但它们不会被自动调用。他们将被定义。在主文件中,只需从导入的模块中调用函数/类。

答案 1 :(得分:3)

导入文件时,将运行python模块中的所有顶级语句。在您的情况下,因为整个编辑文件位于模块级别,所以每个语句都会在导入时运行。我建议你把'edit'命令放在一个函数中:

def run_edit_window():
    ... put your code here

然后在主文件中,您将首先导入编辑文件:

import edit  # Assuming file is named 'edit.py'

然后,当您按下编辑按钮时,请致电:

edit.run_edit_window()