2016-10-15 1 views
0

非常に新しいです。ウィンドウとTkinterの操作に興味があります。優しくしてください。PythonでTkinterを使って新しいウィンドウに切り替える手助けがありますか?

ここに私のコードは、新しいウィンドウに移動し、その新しいウィンドウで画像を開くはずです。しかし、新しいウィンドウにイメージを置くのではなく、最初のウィンドウにイメージを置きます。

これはトップレベルと関係があると思いますが、私はそのことを正しく行うようです。

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() 

答えて

0

このコードを試してみてください。

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() 

これは窓を行いますと、新しいウィンドウをポップアップ表示するためのボタンをプッシュすることができます。例として、そのウインドウにすでにラベルがあります。

+0

ありがとう – Daveyman123

1

別のトップレベルウィジェットを作成するには、Toplevelコマンドを使用します。もう一度Tk()に電話しないでください。例えば以下は、画面上に2つのトップレベルのTkウィンドウ生成

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

をトップtoplevelsとの大きな違いの1つは、あなたがrootを破壊するならば、あなたはTkのをアンロードし、すべてのTkウィンドウを破壊しています。しかし、Toplevelウィジェットはアプリケーションの残りの部分に影響を与えずに破棄することができますので、ダイアログまたは他の別のウィンドウとして使用するのに適しています。

関連する問題