如何从我创建的导入python文件中运行tkinter应用程序?

时间:2017-11-28 15:05:21

标签: python tkinter tkinter-layout

好的,所以我有主窗口(root),这是我的主菜单必不可少的。它有按钮,我希望能够点击并启动另一个窗口。我知道我可以通过将所有代码放在一个文件中来实现这一点,但我被告知这是一个混乱和糟糕的做法?

因此,在我的第二个文档中,我将根窗口放在一个函数中,并在导入它之后在我的主文档中调用它,但得到一个错误:

Traceback (most recent call last):
  File "C:/Users/User/OneDrive/PycharmProjects/PythonProject/main.py", line 22, in <module>
    searchbtn = Button(f1, image=searchImg, text="Search", bg="white")
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2363, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

这是我的代码:

from tkinter import *
import addRemoveWindow

main = Tk()
main.title("Administrator")
main.configure(background="white")



headerLbl = Label(main, text="Main Menu", font=("arial", 50, "bold"), 
fg="powder blue", bg="white")
headerLbl.grid(sticky="NESW", row=0)

f1 = Frame(main, bg="red")
f1.configure(height=35, width=500)
f1.grid(row=1, columnspan=5)

searchImg = PhotoImage(file="searchuser.png")
searchbtn = Button(f1, image=searchImg, text="Search", bg="white")
searchbtn.grid(row=0, sticky=W, pady=5, padx=5)

addRemImg = PhotoImage(file="ddremove.png")
addRembtn = Button(f1, image=addRemImg ,text="Add/Remove", bg="white", 
command=addRemoveWindow.start_add_remove())
addRembtn.grid(row=0, pady=5, padx=5, sticky=E, column=1)

f2 = Frame(main, bg="blue")
f2.configure(height=20)
f2.grid(row=2, columnspan=5)

searchLabel = Label(f2, text="Search")
searchLabel.grid(row=0, sticky=N)
addRemLabel = Label(f2, text="Search")
addRemLabel.grid(row=0, sticky=N, column=1)


main.mainloop()

我不知道出了什么问题。我在addRem按钮中写了命令,但它给了我这个错误,请帮助!

0 个答案:

没有答案