次のようなコード スニペットを試してみます。
directory = 'C:/Users/Geekman2/Documents/Tests/'
...
def AddDirTo(filename)
return directory + filename
次に、投稿したコードは次のようになります。
box1 = AddDirTo('box1.txt') #note: you did close box1's quote on your question
cupcake = Button(donut,text = "Box #1", command = open(box1))
質問のテイズから示されているように、持っているすべてのファイルがテキストファイルである場合は、次のようにすることもできます。
directory = 'C:/Users/Geekman2/Documents/Tests/'
extension = '.txt'
...
def AddDirTo(filename):
return directory + filename + extension
...
box1 = AddDirTo('box1') #note: you did close box1's quote on your question
cupcake = Button(donut,text = "Box #1", command = open(box1))
directory
一番上にあるand変数に反対票を投じようとしている人はextension
、新しい関数を作成することなく、コードを他のディレクトリや拡張機能で再利用できるようになります。