在pyq中打开另一个框架

2024-04-26 07:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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代码,有些是别人写的,它所做的只是打开另一个窗口,而不是框架


Tags: thetextselfappframesinitiscontainer
1条回答
网友
1楼 · 发布于 2024-04-26 07:23:25

这应该对你有用,@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_())

相关问题 更多 >