当前位置: 动力学知识库 > 问答 > 编程问答 >

Can someone help me make the transition to a new window using Tkinter in Python?

问题描述:

Im very new and am interested in working with windows and Tkinter. please be gentle.

here is my code that is supposed to go to a new window then open an image in that new window. However, instead of putting the image in the new window it puts it in the first window.

I think this has something to do with toplevel but i cant seem to make that work right.

import os

from PIL import Image

from PIL import ImageTk

from Tkinter import Tk

import os

class Application(Frame):

""" A GUI application with three buttons. """

def __init__(self, master):

""" Initialize the frame"""

Frame.__init__(self,master)

self.grid()

self.create_widgets()

def create_widgets(self):

""" Create button, text and entry widgets"""

self.instruction = Label(self, text = "First off what is your name?")

self.instruction.grid(row=0, column =0, columnspan=2, sticky=W)

#self.first = first

self.name = Entry(self)

self.name.grid(row=1, column =1, sticky=W)

self.submit_button = Button(self, text ="Submitsss", command = self.reveal)

self.submit_button.grid(row = 2, column = 0, sticky =W)

self.text = Text(self, width =35, height = 5, wrap = WORD)

self.text.grid(row=3, column = 0, columnspan =2, sticky = W)

self.ok_button = Button(self, text = "Next", command = self.go_to_2)

self.ok_button.grid(row = 2, column = 0, sticky = E)

def reveal(self):

"""Display message based on the name typed in"""

content = self.name.get()

message = "Hello %s" %(content)

self.text.insert(0.0, message)

def go_to_2(self):

self.destroy()

#root = Tk()

#self.newApplication = tk.Toplevel(self.master)

#self.app3 = Application2(self.newApplication)

root.title("game")

root.geometry("600x480")

#root = Tk()

#app2=Application2(root)

self.newWindow = Tk.Toplevel()

self.app = Application2(self.newWindow)

class Application2(Frame):

""" A GUI application with three buttons. """

def __init__(self, master):

""" Initialize the frame"""

Frame.__init__(self,master)

self.grid()

self.create_widgets()

#master.panel()

master.title("a")

#self.root.mainloop()

master.img = ImageTk.PhotoImage(Image.open("C:\Users\david\Pictures\sdf.jpg"))

master.panel = Label(root, image = master.img)

master.panel.pack()

def create_widgets(self):

self.submit_button = Button(self, text ="Submit")

self.submit_button.grid(row = 2, column = 0, sticky =W)

self.name = Entry(self)

self.name.grid(row=1, column =1, sticky=W)

self.ok_button = Button(self, text = "Next", command = self.go_to_3)

self.ok_button.grid(row = 2, column = 0, sticky = E)

def go_to_3(self):

root = Tk()

app2=Application3(root)

#def create_picture(self):

class Application3(Frame):

""" A GUI application with three buttons. """

def __init__(self, master):

""" Initialize the frame"""

Frame.__init__(self,master)

self.grid()

self.create_widgets()

def create_widgets(self):

self.button = Button(self, text = "ass")

root.mainloop()

root = Tk()

root.title("game")

root.geometry("600x480")

app = Application(root)

root.mainloop()

#app2= Application2(root)

#if __name__ == '__main__':

# main()

网友答案:

To create another toplevel widget, use the Toplevel command. Don't try calling Tk() again.

For instance the following produces two toplevel Tk windows on screen:

import tkinter as tk
root = tk.Tk()
dlg = tk.Toplevel(root)

One significant difference between the top toplevels is if you destroy root then you unload Tk and destroy all Tk windows. However, a Toplevel widget can be destroyed without affecting the rest of the application so is suitable for use as a dialog or other separate window.

网友答案:

Try this code:

import tkinter as tk

class MainWindow(tk.Frame):
    counter = 0
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.button = tk.Button(self, text="Create new window", 
                                command=self.create_window)
        self.button.pack(side="top")

    def create_window(self):
        self.counter += 1
        t = tk.Toplevel(self)
        t.wm_title("Window #%s" % self.counter)
        l = tk.Label(t, text="This is window #%s" % self.counter)
        l.pack(side="top", fill="both", expand=True, padx=100, pady=100)

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

This will make a window and you can push a button to pop up a new window. There is already a label on that window as an example.

分享给朋友:
您可能感兴趣的文章:
随机阅读: