在pyqt中打开另一个帧

时间:2017-07-17 02:33:00

标签: python pyqt pyqt4

我是PyQt的新手我已经在Tkinter中完成了一些GUI编程,我正在尝试学习如何在单击按钮时打开一个新框架。我在互联网上找到的只是按钮打开另一个窗口。我希望将这个tkinter代码翻译成pyqt4代码

class foo(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        #favicon
        #tk.Tk.iconbitmap(self, default="@/home/sahil/PycharmProjects/gui/fav.ico")

        #changing title of window
        tk.Tk.wm_title(self, "graph")

        #we're building the container that'll contain all the elements
        #Frame is a window
        container = tk.Frame(self)

        #side aligns it to the direction0000000
        #fill fills the entire whitespace
        #exapnd lets you fill the whitespaces if the window is expanded
        container.pack(side="top",fill="both",expand=True)

        #0 is the row number
        #weight is importance
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
self.frames = {}

        #creating a tuple of frames added into the program and passing them through
        for F in (StartPage, PageOne, GraphPage):

            frame = F(container, self)

            #refrencing the page in self.FRAMES
            self.frames[F] = frame

            #North South East West
            frame.grid(row=0, column=0, sticky="nsew")

        #calling a funcction to display the page
        self.show_frame(StartPage)

    def show_frame(self, controller):
        #this corresponds to SELF.FRAMES
        #its looking for the value in FRAMES and raising it
        frame = self.frames[controller]
        #raising the page you called
        frame.tkraise()



class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        newFont = ("Verdana", 12)
        tk.Frame.__init__(self,parent)
        label = tk.Label(self,text="""ALPHA BITCOIN TRADING APPLICATION! There are no warranties for data loss or anytihng""", font=newFont)
        label.pack(padx=10, pady=10)

        button1 = ttk.Button(self, text="Agree", command=lambda: controller.show_frame(GraphPage))
        button1.pack()

        button2 = ttk.Button(self, text="Disagree", command=quit)
        button2.pack()

        button3 = ttk.Button(self, text="PageOne", command=lambda: controller.show_frame(PageOne))
        button3.pack()


app = foo()
app.geometry("800x600")
app.config(background="black")
ani = animation.FuncAnimation(figure, animate, interval=10000)
app.mainloop()

我已经尝试了很多其他人编写的pyqt4代码,所有它只是打开了另一个窗口,而不是一个框架

1 个答案:

答案 0 :(得分:1)

这应该适合你,@ Eyllanesc是对的,你的问题就像你要求教程一样,即使你不是故意的。

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *


class first(QWidget):
    def __init__(self, parent=None):
        super(first, self).__init__(parent)
        # mainwindow.setWindowIcon(QtGui.QIcon('PhotoIcon.png'))
        self.agree = QPushButton('Agree', self)
        self.agree.move(180, 400)
        self.button2 = QPushButton('Disagree', self)
        self.button2.move(270,400)


class second(QWidget):
    def __init__(self, parent=None):
        super(second, self).__init__(parent)
        self.btn = QPushButton("previous", self)
        self.btn.move(100, 350)
        self.greet = QLabel("second",self)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(50, 50, 400, 450)
        self.setFixedSize(400, 450)
        self.setWindowIcon(QIcon("favicon.png"))
        self.startfirst()

    def startsecond(self):
        self.ToolTab = second(self)
        self.setWindowTitle("second")
        self.setCentralWidget(self.ToolTab)
        self.ToolTab.btn.clicked.connect(self.startfirst)
        self.show()

    def startfirst(self):
        self.Window = first(self)
        self.setWindowTitle("first")
        self.setCentralWidget(self.Window)
        self.Window.agree.clicked.connect(self.startsecond)
        self.Window.button2.clicked.connect(QCoreApplication.instance().quit)
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    sys.exit(app.exec_())