0

私はPython初心者で、学校用に簡単なテキストエディタを作ろうとしていますが、保存、切り取り、貼り付け、コピーがうまくいきません。PyWriter と呼ばれ、Tkinter を使用しています。コードのコメントを追加していないことをお詫びします。これが私のコードです:

import Tkinter
from Tkinter import *
from ScrolledText import *
import tkFileDialog
import tkMessageBox

def __init__(self):
    self.text = textPad()
    self.text.pack()
root = Tkinter.Tk(className=" PyWriter")
textPad = ScrolledText(root, width=100, height=80)


def open_command():
    file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Open')
    if file != None:
        contents = file.read()
        textPad.insert('1.0',contents)
        file.close()

def save_command(self):
    file = tkFileDialog.asksaveasfile(parent=root,mode='w',title='Save')
    if file != None:
        data = self.textPad.get('1.0', END+'-1c')
        file.write(data)
        file.close()

def exit_command():
    root.destroy()

def copy_command(self, event=None):
    self.clipboard_clear()
    text = self.get("sel.first", "sel.last")
    self.clipboard_append(text)

def cut_command(self, event=None):
    self.copy()
    self.delete("sel.first", "sel.last")

def paste_command(self, event=None):
    text = self.selection_get(selection='CLIPBOARD')
    self.insert('insert', text)


menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open...", command=open_command)
filemenu.add_command(label="Save...", command=save_command)
filemenu.add_separator()
filemenu.add_command(label="Copy", command=copy_command)
filemenu.add_command(label="Cut", command=cut_command)
filemenu.add_command(label="Paste", command=paste_command)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=exit_command)

textPad.pack()
root.mainloop()
4

2 に答える 2

1

クラスを使用することをお勧めしますが、Tkinter GUI はクラスを使用せずにコーディングされることがよくあります。
おそらくカットアンドペーストが原因で、両方の種類のアプローチが混在しています。
コードのどこが間違っているかを理解するために、2 つの異なるアプローチに従って以下の例を示します。コピー/貼り付けの方法のみを修正しました。残りはあなたの仕事です。

クラスなし (私はこの良いコードを考えていません):

import Tkinter as tk
from ScrolledText import ScrolledText

def copy_command():
    root.clipboard_clear()
    text = textPad.get("sel.first", "sel.last")
    root.clipboard_append(text)

def paste_command():
    text = root.selection_get(selection='CLIPBOARD')
    textPad.insert('insert', text)

root = tk.Tk(className=" PyWriter")

textPad = ScrolledText(root, width=100, height=80)

menu = tk.Menu(root)
root.config(menu=menu)

filemenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Copy", command=copy_command)
filemenu.add_command(label="Paste", command=paste_command)

textPad.pack()
root.mainloop() 

そしてクラスで:

import Tkinter as tk
from ScrolledText import ScrolledText

class TkApp(tk.Tk):
    def __init__(self, className=" PyWriter"):
        tk.Tk.__init__(self, className=className)
        self.textPad = ScrolledText(self, width=100, height=80)
        self.textPad.pack()

        menu = tk.Menu(self)
        self.config(menu=menu)
        filemenu = tk.Menu(menu)
        menu.add_cascade(label="File", menu=filemenu)
        filemenu.add_command(label="Copy", command=self.copy_command)
        filemenu.add_command(label="Paste", command=self.paste_command)

    def copy_command(self):
        self.clipboard_clear()
        text = self.textPad.get("sel.first", "sel.last")
        self.clipboard_append(text)

    def paste_command(self):
        text = self.selection_get(selection='CLIPBOARD')
        self.textPad.insert('insert', text)

if __name__ == '__main__':
    app = TkApp()
    app.mainloop()
于 2013-10-16T19:26:26.770 に答える
0

これは (および他の同様のエラー) が原因です。

def save_command(self):

関数は 1 つの引数を取りますが、ボタンは引数を渡さないため、エラーがスローされます。

私の推測では、このコードを別の場所から誤ってコピーしたのでしょう。classクラスのすべてのマーキングがありますが、ステートメントはありません。__init__クラスをインスタンス化するときにのみ呼び出される関数があり、関数は唯一のパラメーターとして持ってselfいます。これは通常、オブジェクトのメソッドに対して行うことです。

__init__私の提案は、このコードの一部をコピーした場所を詳しく調べることです。おそらく関数の直前に、次のようなものが表示されることがわかります。class Something():

于 2013-10-16T18:47:47.590 に答える