我正在尝试在Tkinter中编写Python GUI程序。我有基本的模板,但我需要它来打开一个文件,当你点击File > Open
。
如何添加这个函数?菜单栏和标签已经在那里了。
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ScrolledText import *
import tkFileDialog
import tkMessageBox
from Tkinter import Tk, Text, BOTH, W, N, E, S, Menu
from ttk import Frame, Button, Label, Style
def main():
root = Tk()
root.geometry("350x300+300+300")
app = Example(root)
root.mainloop()
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Grades")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Grades:")
lbl.grid(sticky=W, pady=4, padx=5)
lbl = Label(self, text="Averagen Grade:")
lbl.grid(row=3, column=3, pady=5)
textPad = ScrolledText(self)
textPad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
abtn = Button(self, text="Save",command=save_command )
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Close")
cbtn.grid(row=2, column=3, pady=4)
hbtn = Button(self, text="Help", command=about_command)
hbtn.grid(row=5, column=0, padx=5)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="Student")
submenu.add_command(label="New Student")
fileMenu.add_cascade(label='Import', menu=submenu, underline=0)
fileMenu.add_command(label="Open...", command=open_command)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
def onExit(self):
self.quit()
def about_command():
label = tkMessageBox.showinfo("About", "Grade keepingn program by Starwarsfan2099")
def save_command(self):
file = tkFileDialog.asksaveasfile(mode='w')
if file != None:
# slice off the last character from get, as an extra return is added
data = self.textPad.get('1.0', END+'-1c')
file.write(data)
file.close()
def open_command():
file = tkFileDialog.askopenfile(mode='rb',title='Select a file')
if file != None:
contents = file.read()
textPad.insert('1.0',contents)
file.close()
if __name__ == '__main__':
main()
textPad
是initUI
方法的局部变量,因此在该方法终止后,对该对象的引用将丢失。换句话说,您不能从initUI
函数之外访问textPad
。
在initUI
方法中,更改以下两行:
textPad = ScrolledText(self)
textPad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
to(使textPad
成为Example
类的字段):
self.textPad = ScrolledText(self)
self.textPad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
我已经改变了你的代码的其他东西,以提高它。具体来说,我已经把那些全局函数的方法变成了Example
等类。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ScrolledText import *
import tkFileDialog
import tkMessageBox
from Tkinter import Tk, Text, BOTH, W, N, E, S, Menu
from ttk import Frame, Button, Label, Style
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Grades")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = Label(self, text="Grades:")
lbl.grid(sticky=W, pady=4, padx=5)
lbl = Label(self, text="Averagen Grade:")
lbl.grid(row=3, column=3, pady=5)
self.textPad = ScrolledText(self)
self.textPad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
abtn = Button(self, text="Save",command=self.save_command)
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Close", command=self.onExit)
cbtn.grid(row=2, column=3, pady=4)
hbtn = Button(self, text="Help", command=self.about_command)
hbtn.grid(row=5, column=0, padx=5)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
menubar = Menu(self.parent)
self.parent.config(menu=menubar)
fileMenu = Menu(menubar)
submenu = Menu(fileMenu)
submenu.add_command(label="Student")
submenu.add_command(label="New Student")
fileMenu.add_cascade(label='Import', menu=submenu, underline=0)
fileMenu.add_command(label="Open...", command=self.open_command)
fileMenu.add_separator()
fileMenu.add_command(label="Exit", underline=0, command=self.onExit)
menubar.add_cascade(label="File", underline=0, menu=fileMenu)
def onExit(self):
self.parent.destroy()
def about_command(self):
label = tkMessageBox.showinfo("About", "Grade keepingn program by Starwarsfan2099")
def save_command(self):
file = tkFileDialog.asksaveasfile(mode='w')
if file != None:
# slice off the last character from get, as an extra return is added
data = self.textPad.get('1.0', 'end-1c')
file.write(data)
file.close()
def open_command(self):
file = tkFileDialog.askopenfile(mode='rb',title='Select a file')
if file != None:
contents = file.read()
self.textPad.insert('1.0', contents)
file.close()
def main():
root = Tk()
root.geometry("350x300+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
main()
无论如何,我鼓励你看看这些变化,这样你就会明白你做得不好的地方。老实说,我还没有完全看完,但我所做的改变在我看来是一个进步。