在命令行中调用另一个文件 (Python)
Call another file in command line (Python)
如何在命令行中调用另一个文件?
实际上我想用调用另一个文件的命令创建按钮。
这是我的代码
from tkinter import *
root = Tk()
root.configure(bg='black') #background color
root.geometry("500x300") #size frame
label1 = Label(text = "Setting" , font = "TIMES 15",fg = "Cyan", bg = "black" ).pack(anchor = NW)
Button1 = Button(root, text = "Done" , fg = "black", bg = "black" , ***command = OpeningPage.foo***).pack(padx = 0 , pady = 0, anchor = NE)
Button2 = Button(root, text = "Cancel" , fg = "black", bg = "black" , command =root.quit).pack(padx = 0, pady =1, anchor = NE)
root.mainloop()
粗体是命令,我希望命令调用我的其他文件。
导入这个文件并调用它的特定函数。
你应该说出你想对文件做什么:只获取文件名,或者像使用 open() 命令一样打开并阅读。
我假设您想打开并阅读该文件,然后打印其中的行。
我使用的是 Python 2.7,所以不是导入 tkFileDialog,而是导入 filedialog 和 tkinter 而不是 Tkinter,如 Tkinter site. I looked at this Python Course 所述,并找到了一些带有按钮的示例代码,因为我自己不太记得如何做 :P,然后我刚刚添加了您的一些功能。
from Tkinter import *
from tkFileDialog import askopenfile
root = Tk()
root.configure(bg='black') #background color
root.geometry("500x300") #size frame
def callback():
name= askopenfile(mode='r')
file = name
count = 0
for line in file:
count += 1
print count,line.rstrip()
label1 = Label(text = "Setting" , font = "TIMES 15",fg = "Cyan", bg="black" ).pack(anchor = NW)
errmsg = 'Error!'
Button(text='File Open', command=callback, fg = "Cyan", bg = "black" ).pack(fill=X,padx = 0 , pady = 0, anchor = NE)
Button(text='Quit', command=root.quit , fg = "Cyan", bg = "black" ).pack(fill=X,padx = 0 , pady = 0, anchor = NE)
mainloop()
希望这对你有用。
如果这就是你的意思,从这个文件到 运行 另一个 Python 文件,你需要 import os
然后当你想 运行 它调用 os.system("python yourfile.py")
。它基本上调用引号之间的任何命令,就好像来自 terminal/cmd。
我还建议创建这样的按钮,而不是像您那样创建按钮。
button=Button(root)
Button.pack()
这是因为按照您的方式创建它们,因为正如 7stud 所说,"their values will be whatever pack() returns, which is None."
如何在命令行中调用另一个文件?
实际上我想用调用另一个文件的命令创建按钮。
这是我的代码
from tkinter import *
root = Tk()
root.configure(bg='black') #background color
root.geometry("500x300") #size frame
label1 = Label(text = "Setting" , font = "TIMES 15",fg = "Cyan", bg = "black" ).pack(anchor = NW)
Button1 = Button(root, text = "Done" , fg = "black", bg = "black" , ***command = OpeningPage.foo***).pack(padx = 0 , pady = 0, anchor = NE)
Button2 = Button(root, text = "Cancel" , fg = "black", bg = "black" , command =root.quit).pack(padx = 0, pady =1, anchor = NE)
root.mainloop()
粗体是命令,我希望命令调用我的其他文件。
导入这个文件并调用它的特定函数。
你应该说出你想对文件做什么:只获取文件名,或者像使用 open() 命令一样打开并阅读。
我假设您想打开并阅读该文件,然后打印其中的行。 我使用的是 Python 2.7,所以不是导入 tkFileDialog,而是导入 filedialog 和 tkinter 而不是 Tkinter,如 Tkinter site. I looked at this Python Course 所述,并找到了一些带有按钮的示例代码,因为我自己不太记得如何做 :P,然后我刚刚添加了您的一些功能。
from Tkinter import *
from tkFileDialog import askopenfile
root = Tk()
root.configure(bg='black') #background color
root.geometry("500x300") #size frame
def callback():
name= askopenfile(mode='r')
file = name
count = 0
for line in file:
count += 1
print count,line.rstrip()
label1 = Label(text = "Setting" , font = "TIMES 15",fg = "Cyan", bg="black" ).pack(anchor = NW)
errmsg = 'Error!'
Button(text='File Open', command=callback, fg = "Cyan", bg = "black" ).pack(fill=X,padx = 0 , pady = 0, anchor = NE)
Button(text='Quit', command=root.quit , fg = "Cyan", bg = "black" ).pack(fill=X,padx = 0 , pady = 0, anchor = NE)
mainloop()
希望这对你有用。
如果这就是你的意思,从这个文件到 运行 另一个 Python 文件,你需要 import os
然后当你想 运行 它调用 os.system("python yourfile.py")
。它基本上调用引号之间的任何命令,就好像来自 terminal/cmd。
我还建议创建这样的按钮,而不是像您那样创建按钮。
button=Button(root)
Button.pack()
这是因为按照您的方式创建它们,因为正如 7stud 所说,"their values will be whatever pack() returns, which is None."