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

Python: Imported Tkinter program runs itself?

所以我正在为我的最后一年项目编写一个程序,尤其是这件事让我很烦。我已经研究了这么久,但我想不通,所以我希望有新人能够阐明它!

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

到目前为止,我已经完成了基本的 windows 编码。我有两个 windows:一个主要 window 和一个 "edit" window 用于更改参与者姓名。我在单独的 .py 文件中编写了这两个 windows 的代码,并导入了“edit” window文件到主文件中,这样当在主 window 中单击“EDIT”按钮时,我可以将其调用到 运行。然而,无论出于何种原因,当我 运行 主程序时,“edit” window 只是 运行s 本身,即使唯一引用它是 import editWindow.

这是主要代码 window:

'''
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()

...以及“edit”的代码 window:

'''
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 文件中是否真的更好,尽管我认为这样做会更优雅。

如果有人可以为此建议一个很好的、简化的解决方案,将两个 windows 的代码分开,我将不胜感激。我正在使用 Python 3.4,仅供参考。

谢谢。

当您导入一个模块时,您正在执行它的代码。 python 中导入模块的实际工作方式。要停止它,请将代码放入函数或 class 中的导入模块中。公平地说,它仍然是 运行 make 函数或 classes,但它们不会被自动调用。它们将被定义。在主文件中,您只需从导入的模块中调用 functions/class。

导入文件时,python 模块中的所有顶级语句都是 运行。在您的情况下,因为整个编辑文件都处于模块级别,所以每个语句在导入时都会得到 运行 。我建议采用您的 'edit' 命令,然后将其放入函数中:

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

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

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

然后,当您按下编辑按钮时,调用:

edit.run_edit_window()