0
クリックしたボタンによってページが切り替わるGUIで複数のフレームを使用したいと思います。私はすでにこれについていくつかのスレッドがあることを知っており、私はthis oneを見てきました。 しかし、私のページでは、フレームごとにキャンバスに異なるイメージが必要なので、別のフレームを上げると、そのキャンバスに新しいキャンバスと新しいイメージが表示されます。私はたくさん試しましたが、キャンバスがイメージで表示されるようにする方法を知らない。Tkinter複数のフレーム/ 'ページ':キャンバスイメージを使用する
はここでほとんどがリンクの上からコピーし、私がこれまで持っているものです。
import tkinter as tk # python3
TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
self.frames["StartPage"] = StartPage(parent=container, controller=self)
self.frames["PageOne"] = PageOne(parent=container, controller=self)
self.frames["PageTwo"] = PageTwo(parent=container, controller=self)
self.frames["StartPage"].grid(row=0, column=0, sticky="nsew")
self.frames["PageOne"].grid(row=0, column=0, sticky="nsew")
self.frames["PageTwo"].grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self._canvas = tk.Canvas(parent, bg='white', width=900, height=3517, scrollregion=(0, 2800, 100, 800))
self._photo = tk.PhotoImage(file='images/homegraphic.gif')
self._canvas.create_image(0, 0, image=self._photo, anchor='nw')
label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
は、どのように私はキャンバスの画像を表示するのですか?私はこれを理解しようと長い時間を費やしており、どんな助けにも感謝しています!
私はクラススタートページにキャンバスのグリッドを行う際に、追加するのを忘れ、窓は右のサイズをポップアップする以外は何も示していない、テキスト付きの普通のグレーのTkinterの背景上部にボタンがあります。 – amrungwaew
あなたのキャンバスは、あなたのボタンと同じように、現在のフレーム自体を親ではなくマスターとして使う必要があります。 'self._canvas = tk.Canvas(self、bg = 'white'、width = 900、height = 3517、scrollregion = (0,2800,100,800)) 'となる。そして、StartPageはpackによって管理されているので、あなたはレイアウトのために 'pack'を使う必要があります。 – Novel