2

私はPython2.7でこのコードを使用して、ファイルを開くためのtkinterを作成しButtonます。FrameそのLabel下にあります。私はそれを作ろうとしているので、ファイルが開かれると、ラベルはパス「file1.name」などをに印刷します。Label新しいファイルを開くと、それがLabel再び変更されます。

また、ここで使用している関数よりも関数間でデータを移動する方が良い方法があると思いますがglobal、今は心配していません。

開いているファイルのデータを関数間で移動して、データを混合して新しいファイルに保存できるようにする必要があります。コードは次のとおりです。

from Tkinter import *
import Tkinter
import tkFileDialog
import tkMessageBox
root = Tkinter.Tk()
global rfile1
global rfile2
rfile1 = ""
rfile2 = ""
class Application(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets1()
        self.createLabels1()
        self.createWidgets2()
        self.createLabels2()

    def createWidgets1(self):
        self.oButton = Button(self, text = "open1", command = self.openfile1)
        self.oButton.grid()

    def createLabels1(self):
        self.oLabel = Label(self, text = "whoops")
        self.oLabel.grid()

    def createWidgets2(self):
        self.oButton = Button(self, text = "open2", command= self.openfile2)
        self.oButton.grid()

    def createLabels2(self):
        self.oLabel2 = Label(self, text = "whoops2")
        self.oLabel2.grid()

    def openfile1(self):
        file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb')

        rfile1 = file1.read()
        tkMessageBox.showinfo("oy", rfile1, parent=root)

    def openfile2(self):
        file2 = tkFileDialog.askopenfile(parent=root, mode='rb')

        rfile2 = file2.read()
        tkMessageBox.showinfo("hola", rfile2, parent=root)

app = Application()
app.master.title("whiggy whompus")
app.mainloop()
4

2 に答える 2

0

@mgilson が最初の質問を解決しました。globals を使用せずに関数間でパラメーターを渡す方法に関する 2 番目の質問:

変数をアプリケーションクラスの属性として保存することを検討してください。

構文自体。現在のインスタンスの属性です(インスタンスはクラスの特定の例です-あなたの車がクラス「車」の特定の例であるように)。この例では、インスタンス属性をグローバルであるかのように使用できます。

from Tkinter import *
import Tkinter
import tkFileDialog
import tkMessageBox
root = Tkinter.Tk()

class Application(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets1()
        self.createLabels1()
        self.createWidgets2()
        self.createLabels2()
        self.rfile1 = ""
        self.rfile2 = ""

    def createWidgets1(self):
       self.oButton = Button(self, text = "open1", command = self.openfile1)
       self.oButton.grid()

    def createLabels1(self):
        self.oLabel = Label(self, text = "whoops")
        self.oLabel.grid()

    def createWidgets2(self):
        self.oButton = Button(self, text = "open2", command= self.openfile2)
        self.oButton.grid()

    def createLabels2(self):
        self.oLabel2 = Label(self, text = "whoops2")
        self.oLabel2.grid()

    def openfile1(self):
        file1 = tkFileDialog.askopenfile(title = "choose a file, *****", parent=root, mode = 'rb')

        self.rfile1 = file1.read()
        tkMessageBox.showinfo("oy", self.rfile1, parent=root)

    def openfile2(self):
        file2 = tkFileDialog.askopenfile(parent=root, mode='rb')

        self.rfile2 = file2.read()
        tkMessageBox.showinfo("hola", self.rfile2, parent=root)

app = Application()
app.master.title("whiggy whompus")
app.mainloop()
于 2014-07-07T08:59:43.063 に答える