ここで非常に簡単な質問があります。Tkinter(python)では、登録ページやログインページなど、アプリケーションのさまざまなページに移動するためにボタンを使用するのは誰かと思っていました。GUIにはウェブサイトのような「ページ」がないことを知っています。いくつかの異なる方法を見てきましたが、異なるページへのリンクを作成するための最良の方法は何ですか?
72983 次
3 に答える
49
各ページをフレームにします。次に、ボタンで行う必要があるのは、表示されているものをすべて非表示にしてから、目的のフレームを表示することです。
これを行う簡単な方法は、フレームを互いに積み重ねて(これは理にplace
かなっている場合は一度だけです)、次に、lift()
表示したいフレームを積み重ねることです。この手法は、すべてのページが同じサイズの場合に最適に機能します。実際には、フレームを含むサイズを明示的に設定する必要があります。
以下は、考案された例です。これが問題を解決する唯一の方法ではありません。解決するのが特に難しい問題ではないことを証明するだけです。
import Tkinter as tk
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 1")
label.pack(side="top", fill="both", expand=True)
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 2")
label.pack(side="top", fill="both", expand=True)
class Page3(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="This is page 3")
label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
p3 = Page3(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)
b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)
b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)
b1.pack(side="left")
b2.pack(side="left")
b3.pack(side="left")
p1.show()
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("400x400")
root.mainloop()
于 2013-02-11T19:10:24.930 に答える
5
このようなことをしてもらえますか?
import tkinter
def page1():
page2text.pack_forget()
page1text.pack()
def page2():
page1text.pack_forget()
page2text.pack()
window = tkinter.Tk()
page1btn = tkinter.Button(window, text="Page 1", command=page1)
page2btn = tkinter.Button(window, text="Page 2", command=page2)
page1text = tkinter.Label(window, text="This is page 1")
page2text = tkinter.Label(window, text="This is page 2")
page1btn.pack()
page2btn.pack()
page1text.pack()
それは私にはずっと簡単に思えます。
于 2018-06-30T19:50:23.060 に答える
-1
import tkinter as tk
root=tk.Tk()
root.geometry("360x360")
frame=tk.Frame(root,bg='lightblue')
frame.place(relx=0.2,rely=0.2,relheight=0.6,relwidth=0.6)
def page1():
label=tk.Label(frame,text='this is the page1')
label.place(relx=0.3,rely=0.4)
def page2():
label=tk.Label(frame,text='this is the page2')
label.place(relx=0.3,rely=0.4)
def page3():
label=tk.Label(frame,text='this is the page3')
label.place(relx=0.3,rely=0.4)
bt=tk.Button(root,text='page1',command=page1)
bt.grid(column=0,row=0)
bt1=tk.Button(root,text='page2',command=page2)
bt1.grid(row=0,column=1)
bt2=tk.Button(root,text='page3',command=page3)
bt2.grid(row=0,column=2)
root.mainloop()`
于 2019-12-10T12:01:22.053 に答える