Tkinter:如何在页面上显示带有按钮的页面? [英] Tkinter: How to show pages with buttons on page?

查看:28
本文介绍了Tkinter:如何在页面上显示带有按钮的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个欺凌/虐待项目编写一个热线"程序,有 4 页:主页/欺凌/虐待/其他和一个顶部栏.我正在放置按钮以进入其他页面,但每次都会出现不同的错误.

I'm writing a "hotline" program for a bullying/abuse project, with 4 pages: Home/Bulling/Abuse/Other and a top bar. I am putting buttons to get to the other pages, but there is a different error every time.

我已尝试从页面和容器中执行提升命令,以及编程中的显示命令.均无效.(顺便说一句,代码不是我的,大部分来自问题 使用 Tkinter 中的按钮导航到应用程序的不同页面? 作者:Caspar Wylie)

I have tried the lift command from the page and the container, as well as the show command programmed in. Neither work. (By the way, the code is not all mine, much of it was from the question Using buttons in Tkinter to navigate to different pages of the application? by Caspar Wylie)

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="Welcome to the Help Hotline! Please select your concern.", relief = 'groove')
       label.grid(row = 0, column = 1, columnspan = 3, sticky = 'nesw', padx = 5, pady = 5)

       bullybutton = tk.Button(self, text = "Bullying",  command = MainView.p2.lift)
       bullybutton.grid(row = 1, column = 1, padx = 5, pady = 5, sticky = 'nsew')

       abusebutton = tk.Button(self, text = "Abuse",  command = MainView.p3.lift)
       abusebutton.grid(row = 1, column = 2, padx = 5, pady = 5, sticky = 'nsew')

       otherbutton = tk.Button(self, text = "Other",  command = MainView.p4.lift)
       otherbutton.grid(row = 1, column = 3, padx = 5, pady = 5, sticky = 'nsew')

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.grid(row = 0, column = 1, sticky = 'nesw', padx = 5, pady = 5)

class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 3")
       label.grid(row = 0, column = 1, sticky = 'nesw', padx = 5, pady = 5)

class Page4(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 4")
       label.grid(row = 0, column = 1, sticky = 'nesw', padx = 5, pady = 5)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)
        p4 = Page4(self)

        container = tk.Frame(self)
        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)
        p4.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        menubar = tk.Menu(root)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Home", command=p1.lift)
        filemenu.add_command(label="Bullying", command=p2.lift)
        filemenu.add_command(label="Abuse", command=p3.lift)
        filemenu.add_command(label="Other", command=p4.lift)
        filemenu.add_separator()
        filemenu.add_command(label="Quit", command=exit)
        menubar.add_cascade(label="Navigate", menu=filemenu)

        root.config(menu=menubar)
        p1.show()


root = tk.Tk()
root.geometry('500x500')
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.title("Bullying and Abuse Hotline")
root.mainloop()

我希望有 3 个按钮,每个按钮都转到自己的页面,但我收到关于无属性 tk"或缺少参数 self"或根本没有更改的错误.

I want there to be 3 buttons, each going to their own page, but I get an error about "no attribute tk" or "missing argument self" or no change at all.

推荐答案

这是对您的代码的(相当重要的)返工,我认为它可以满足您的需求.您没有提供 Caspar Wylie 问题的链接,因此我大量借鉴了 Bryan Oakley 的开创性answer 到问题 在 tkinter 中的两个帧之间切换,尽管它做了非常相似的事情没有菜单(但最后包含一些可能非常有用的链接).

Here's a (fairly major) reworking of your code that I think makes it do what you want. You didn't provide a link to Caspar Wylie's question, so I've borrowed heavily on the techniques shown in Bryan Oakley's seminal answer to the question Switch between two frames in tkinter that does very similar things although doesn't feature a menu (but does contain a number of possibly very useful links at the end).

更新

我自己找到了 Caspar Wylie 的问题,并在您的问题中添加了指向它的链接,并稍微修改了下面的代码以使其与相关答案更加一致(恰好也是由 Bryan 编写的).

Found Caspar Wylie's question myself and added link to it in your question and have modified the code below slightly to be more consistent to the associated answer (which happens to also have been written by Bryan).

import tkinter as tk


class MainView(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)

        # Create dictionary of page (Frame subclass) instances.
        self.pages  = {}
        for Page in (Home, Bullying, Abuse, Other):
            page = Page(container, controller=self)
            self.pages[Page.__name__] = page
            page.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        menubar = tk.Menu(parent)
        filemenu = tk.Menu(menubar, tearoff=0)

        def options(page_name, show=self.show):  # Helper func.
            return dict(label=page_name, command=lambda: show(page_name))

        filemenu.add_command(**options('Home'))
        filemenu.add_command(**options('Bullying'))
        filemenu.add_command(**options('Abuse'))
        filemenu.add_command(**options('Other'))

        filemenu.add_separator()
        filemenu.add_command(label='Quit', command=exit)
        menubar.add_cascade(label='Navigate', menu=filemenu)

        parent.config(menu=menubar)
        self.show('Home')  # Display initial page.

    def show(self, page_name):
        self.pages[page_name].lift()


class Home(tk.Frame):
   def __init__(self, parent, controller):
       super().__init__(parent)

       label = tk.Label(self, text='Welcome to the Help Hotline! '
                                   'Please select your concern.', relief='groove')
       label.grid(row=0, column=1, columnspan=3, sticky='nesw', padx=5, pady=5)

       def options(page_name, show=controller.show):  # Helper func.
           return dict(text=page_name, command=lambda: show(page_name))

       bully_button = tk.Button(self, **options('Bullying'))
       bully_button.grid(row = 1, column=1, padx=5, pady=5, sticky='nsew')

       abuse_button = tk.Button(self, **options('Abuse'))
       abuse_button.grid(row=1, column=2, padx=5, pady=5, sticky='nsew')

       other_button = tk.Button(self, **options('Other'))
       other_button.grid(row=1, column=3, padx=5, pady=5, sticky='nsew')


class Bullying(tk.Frame):
   def __init__(self, parent, controller):
       super().__init__(parent)

       label = tk.Label(self, text='This is page 2')
       label.grid(row=0, column=1, sticky='nesw', padx=5, pady=5)


class Abuse(tk.Frame):
   def __init__(self, parent, controller):
       super().__init__(parent)

       label = tk.Label(self, text='This is page 3')
       label.grid(row=0, column=1, sticky='nesw', padx=5, pady=5)


class Other(tk.Frame):
   def __init__(self, parent, controller):
       super().__init__(parent)

       label = tk.Label(self, text='This is page 4')
       label.grid(row=0, column=1, sticky='nesw', padx=5, pady=5)


root = tk.Tk()
root.geometry('500x500')
main = MainView(root)
main.pack(side='top', fill='both', expand=True)
root.title('Bullying and Abuse Hotline')
root.mainloop()

这篇关于Tkinter:如何在页面上显示带有按钮的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆