Issue
I am new to PyQt I have done some GUI programming in Tkinter, I am trying to learn how to open a new frame when I click a button. All I could find on the internet was buttons opening another window all together. I want a translation of something like this tkinter code to pyqt4 code
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()
I have tried a lot of pyqt4 code some written by others and all it did was just open another window, not a frame
Solution
this should work for you and @Eyllanesc is right your question is framed like you're asking for a tutorial even if you don't mean it to be.
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_())
Answered By - user5530332
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.