-1

i have written the "instruction" for my tkinter program on a .txt file and when the Instruction button is pressed, I would like a separate window to be created with the instruction copied from the text file into the output box.

I would be very grateful if anyone could help me. this is what I have done so far but it doesn't work at all.

instructionBtn = Button(window, text="?", command=start, height = 5, width  =30,fg="black", bg="lime green", font=("Comic Sans MS", 10))
instructionBtn.grid(row=29, column=1, sticky=S)

window.mainloop()

def instruction():
    instructionwindow = Tk() #create window 
    instructionwindow.geometry("500x350")

    instructionwindow.title("Instruction")
    instructionwindow.configure(background='white')

    instructionFile=open("Instruction.txt","r")
    instruction.read


textboxOutput = Text(window, wrap="Instruction.txt", width = 150, height = 20)
textboxOutput.grid(row = 20, column = 0, columnspan=10)


instruction.mainloop() 
furas
  • 134,197
  • 12
  • 106
  • 148
tamilini
  • 53
  • 1
  • 2
  • 10
  • program should have only one `Tk()` window - main window. Other widnows create with `Toplevel()`. And it should have only one `mainloop()`. And put code before `mainloop()` - everything after `mainloop()` is executed after you close program. – furas Dec 01 '17 at 20:50
  • `instruction.read` is very different from `instruction.read()`. – Bryan Oakley Dec 01 '17 at 20:51
  • where is function `start()` which you assign with `command=start` ? – furas Dec 01 '17 at 20:51
  • example how to open [second window](https://github.com/furas/python-examples/blob/master/tkinter/toplevel/main-1.py) – furas Dec 01 '17 at 20:54

1 Answers1

1

When I want a second window, often a message box will do the trick, like so:

from Tkinter import *
from tkMessageBox import showinfo

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.instr = Button(frame, text="Instruction", command=self.instruction)
        self.instr.pack(side=LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instructions available'
        showinfo("Instructions", message)

root = Tk()
app = App(root)
root.mainloop()

Or, if you prefer an OOP style:

# template: https://stackoverflow.com/a/17470842/8747

# Use Tkinter for python 2, tkinter for python 3
import Tkinter as tk
import tkMessageBox


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.button = tk.Button(
            self, text="QUIT", fg="red", command=self.quit)

        self.instr = tk.Button(
            self, text="Instruction", command=self.instruction)

        self.button.pack(side=tk.LEFT)
        self.instr.pack(side=tk.LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instruction available'

        msg = tkMessageBox.showinfo("Instruction", message)

if __name__ == "__main__":
    root = tk.Tk() 
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

Of course, sometimes a message box isn't flexible enough, and you need to create a top-level window:

# template: https://stackoverflow.com/a/17470842/8747

# Use Tkinter for python 2, tkinter for python 3
import Tkinter as tk
import tkMessageBox


class Instruction(tk.Toplevel):
    def __init__(self, parent, message, *args, **kwargs):
        tk.Toplevel.__init__(self, parent, *args, **kwargs)

        self.msg = tk.Message(self, text=message)
        self.button = tk.Button(self, text="Dismiss", command=self.destroy)

        self.msg.pack()
        self.button.pack()

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.button = tk.Button(
            self, text="QUIT", fg="red", command=self.quit)

        self.instr = tk.Button(
            self, text="Instruction", command=self.instruction)

        self.button.pack(side=tk.LEFT)
        self.instr.pack(side=tk.LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instruction available'

        msg = Instruction(self, message)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
Robᵩ
  • 163,533
  • 20
  • 239
  • 308