I'm trying to display an image in pyqt for my coursework. I'm attempting this in the Handle Question sub routine. here's a sample of it
class IntegrationQuestions(QtGui.QMainWindow):
def __init__(self, parent = None):
from equation import IntQuestion, IntAnswer
QtGui.QDialog.__init__(self, parent)
self.setWindowTitle('Simple Integration')
self.setMinimumSize(265,400)
self.lbl1 = QtGui.QLabel("Integrate the equation below",self)
self.lbl1.move(0,0)
self.lbl1.resize(200,20)
self.lbl2 = QtGui.QLabel(pretty(IntQuestion[0], use_unicode = False), self)
self.lbl2.resize(200, 80)
self.lbl2.move(30,30)
self.lbl3 = QtGui.QLabel("Sketch pad",self)
self.lbl3.move(0,120)
self.SketchPad = QtGui.QLineEdit(self)
self.SketchPad.resize(250,150)
self.SketchPad.move(0,150)
self.lbl4 = QtGui.QLabel("Answer",self)
self.lbl4.move(0,300)
self.Answer = QtGui.QLineEdit(self)
self.Answer.move(0,330)
self.Answer.resize(250,20)
self.next_question.clicked.connect(self.HandleQuestion)
this is where I'm attempting to add in a question
def HandleQuestion(self):
pic = QtGui.QLabel(self)
pic.setPixmap(QtGui.QPixmap("Q107.png"))
self.lbl3.move(0,190)
self.SketchPad.resize(250,80)
self.SketchPad.move(0,220)
You initialized everything properly, however you never set the label to be shown.
def HandleQuestion(self):
pic = QtGui.QLabel(self)
pic.setPixmap(QtGui.QPixmap("Q107.png"))
pic.show() # You were missing this.
self.lbl3.move(0,190)
self.SketchPad.resize(250,80)
self.SketchPad.move(0,220)
Related
Here i have created UI using pyqt4 for one file processing method. OOPs concept used here for the implementation. First select the path by using browse button and the path inserted into text box
from PyQt4 import QtGui, QtCore
import sys
from PyQt4.QtGui import QLabel, QPixmap
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.labl = QtGui.QLabel(self)
self.setGeometry(550, 200, 550, 300)
self.setWindowTitle('-VDT')
self.logo=QLabel(self)
self.logo.setPixmap(QPixmap("logo.jpg"))
self.logo.move(130, 30)
self.browse_btn = QtGui.QPushButton('Browse', self)
self.browse_btn.move(430, 110)
self.start_btn = QtGui.QPushButton('Start', self)
self.start_btn.move(150, 200)
self.textbox = QtGui.QLineEdit(self)
self.textbox.move(80, 110)
self.textbox.resize(330, 25)
self.browse_btn.clicked.connect(self.browse)
self.start_btn.clicked.connect(self.start)
self.dialog = Second(self)
def browse(self):
file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
self.textbox.setText(file)
def start(self):
# self.dialog.setGeometry(550, 200, 550, 300)
# self.dialog.show()
dir_path= self.textbox.text()
print(dir_path)
self.start_btn.deleteLater()
self.browse_btn.deleteLater()
self.textbox.deleteLater()
self.label1 = QtGui.QLabel(self)
self.label1.setText("Started Processing")
self.label1.move(30, 110)
self.label2 = QtGui.QLabel(self)
self.label2.setText("Total files")
self.label2.move(30, 160)
self.label3 = QtGui.QLabel(self)
self.label3.setText("Processed Files")
self.label3.move(30, 210)
Total_no_of_files= number
self.label4 = QtGui.QLabel(self)
self.label4.setText(Total_no_of_files) #Have to add number of files in the selected folder
self.label4.move(240, 160)
self.label5 = QtGui.QLabel(self)
self.label5.setText(Processed_no_of_files) # Have to add number of files processing, this value will increment from 1 to total number of files
self.label5.move(30, 210) #
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
While clicking the start button, the text box and buttons are removed by using deletelater() method. Then i have added few labels. Following are the issues.
1) The image logo small portion is showing
2) Newly added labels are not showing
3) The newly added label5 value have to be change while it going through the process in a for loop. (in tkinter i have used intvar() and root.update() to do this)
How can i solve this issues?
Updated
I have changed my code based on your answer, but the buttons and text boxes are not deleted and now the labels are over lapping with the old buttons and text box. Please see the following code
from PyQt4 import QtGui, QtCore
import sys
import os
from PyQt4.QtGui import QLabel, QPixmap
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.labl = QtGui.QLabel(self)
self.setGeometry(550, 200, 550, 300)
self.setWindowTitle('-VDT')
self.logo=QLabel(self)
self.logo.setPixmap(QPixmap("logo.jpg"))
self.logo.move(130, 30)
self.logo.setVisible(True)
self.browse_btn = QtGui.QPushButton('Browse', self)
self.browse_btn.move(430, 110)
self.start_btn = QtGui.QPushButton('Start', self)
self.start_btn.move(150, 200)
self.textbox = QtGui.QLineEdit(self)
self.textbox.move(80, 110)
self.textbox.resize(330, 25)
self.browse_btn.clicked.connect(self.browse)
self.start_btn.clicked.connect(self.start)
self.dialog = Second(self)
def browse(self):
file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
self.textbox.setText(file)
def start(self):
# self.dialog.setGeometry(550, 200, 550, 300)
# self.dialog.show()
dir_path= self.textbox.text()
print(dir_path)
list_dir = os.listdir(dir_path)
dir_length = len(list_dir)
self.start_btn.deleteLater()
self.browse_btn.deleteLater()
self.textbox.deleteLater()
self.logo.setVisible(True)
self.label1 = QtGui.QLabel(self)
self.label1.setText("Started Processing")
self.label1.move(30, 110)
self.label1.setVisible(True)
self.label2 = QtGui.QLabel(self)
self.label2.setText("Total files")
self.label2.move(30, 160)
self.label2.setVisible(True)
self.label3 = QtGui.QLabel(self)
self.label3.setText("Processed Files")
self.label3.move(30, 210)
self.label3.setVisible(True)
Total_no_of_files= dir_length
self.label4 = QtGui.QLabel(self)
self.label4.setText(str(dir_length)) #Have to add number of files in the selected folder
self.label4.move(240, 160)
self.label4.setVisible(True)
self.label5 = QtGui.QLabel(self)
self.label5.move(240, 210)
self.label5.setVisible(True)
path = dir_path
TEST_IMAGE_PATHS = [f for f in os.listdir(path) if f.endswith('.jpg')]
# print(TEST_IMAGE_PATHS)
Total_No_Files = len(TEST_IMAGE_PATHS)
import cv2
comp_no_files=0
for filename in TEST_IMAGE_PATHS:
image_path= path+'//'+filename
print(image_path)
image = cv2.imread(image_path)
img = cv2.resize(image, (660, 340))
cv2.imshow('Show Image', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
comp_no_files += 1
self.label5.setText(str(comp_no_files))
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You setVisible(True) on every labels.
Why does labels not show after deleteLater()?
deleteLater() makes no difference about your problem.
In python, basically, you prepare the settings before you show them.
setGeometry(),setPixmap(),set..... all of settings.
and when the parent widget shows, all of the settings are shown.
But the labels on the problem, the window has already been shown.
So, you must setVisible(True) clearly.
That is to say, you set the labels at the first time, in init constructor, the labels shows up soon.
I think there is exception about basic thought of python, I have no idea except for the reason.
1.2 is solved by setVisible(True).
for question 3,
It is important measure the number of directory.
label5 issue
Basically, setText() is executed once.
I recommend that you sometimes customize your code for counting the number during processing some actions,but it is not the time now.Because there is no necessity for doing it.
If you want to show the changing of the number,you execute it in the sequence or timeline.
If your folda is very big, and a great number of foldas you have, you can see naturally the changing for or while sequence.
for num in sequence:
label5.setText(str(num))
but I understood you wanted to show the changing at any rate, so I used QTimeLine
from 0 to dir_length, 2000ms, the number will be changed after QTimeLine.start()
in the code.
It depends on you when you connect this way if you count the number of foldas.
I think I cannot say it clearly now.
I don't explain how to do it here.
please add these command.
import os #at the first line of your editor.
-----------------------------
dir_path= self.textbox.text() # under the function of start
print(dir_path)
list_dir = os.listdir(dir_path)
dir_length = len(list_dir)
on my occasion , as the result, please try this code.
Updated
In my case, it had no problem.Buttons and textboxes are hidden completely.
But in your case, it remains after pushing start-button.
I tried to change your code from deleteLater() to setVisible(False)
self.start_btn.setVisible(False
self.browse_btn.setVisible(False)
self.textbox.setVisible(False)
If you can't solve the problem,please give me a comment.
import os
from PyQt4.QtGui import QLabel, QPixmap
class Second(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Second, self).__init__(parent)
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
super(First, self).__init__(parent)
self.labl = QtGui.QLabel(self)
self.setGeometry(550, 200, 550, 300)
self.setWindowTitle('-VDT')
self.logo=QLabel(self)
self.logo.setPixmap(QPixmap("logo.jpg"))
self.logo.move(130, 30)
self.logo.setVisible(True)
self.browse_btn = QtGui.QPushButton('Browse', self)
self.browse_btn.move(430, 110)
self.start_btn = QtGui.QPushButton('Start', self)
self.start_btn.move(150, 200)
self.textbox = QtGui.QLineEdit(self)
self.textbox.move(80, 110)
self.textbox.resize(330, 25)
self.browse_btn.clicked.connect(self.browse)
self.start_btn.clicked.connect(self.start)
self.dialog = Second(self)
def browse(self):
file = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
self.textbox.setText(file)
def start(self):
# self.dialog.setGeometry(550, 200, 550, 300)
# self.dialog.show()
dir_path= self.textbox.text()
print(dir_path)
list_dir = os.listdir(dir_path)
dir_length = len(list_dir)
self.start_btn.setVisible(False)
self.browse_btn.setVisible(False)
self.textbox.setVisible(False)
self.logo.setVisible(True)
self.label1 = QtGui.QLabel(self)
self.label1.setText("Started Processing")
self.label1.move(30, 110)
self.label1.setVisible(True)
self.label2 = QtGui.QLabel(self)
self.label2.setText("Total files")
self.label2.move(30, 160)
self.label2.setVisible(True)
self.label3 = QtGui.QLabel(self)
self.label3.setText("Processed Files")
self.label3.move(30, 210)
self.label3.setVisible(True)
Total_no_of_files= dir_length
self.label4 = QtGui.QLabel(self)
self.label4.setText(str(dir_length)) #Have to add number of files in the selected folder
self.label4.move(240, 160)
self.label4.setVisible(True)
self.label5 = QtGui.QLabel(self)
self.label5.move(240, 210)
self.label5.setVisible(True)
path = dir_path
TEST_IMAGE_PATHS = [f for f in os.listdir(path) if f.endswith('.jpg')]
# print(TEST_IMAGE_PATHS)
Total_No_Files = len(TEST_IMAGE_PATHS)
import cv2
comp_no_files=0
for filename in TEST_IMAGE_PATHS:
image_path= path+'//'+filename
print(image_path)
image = cv2.imread(image_path)
img = cv2.resize(image, (660, 340))
cv2.imshow('Show Image', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
comp_no_files += 1
self.label5.setText(str(comp_no_files))
def main():
app = QtGui.QApplication(sys.argv)
main = First()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
after updating processed file is 1.because I have a jpg file.
I want to load an image If I click on a button but only a little tiny pixel of the image appears.
It looks like that:
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(1000, 1000)
self.setWindowTitle("MyWindow")
self.setWindowIcon(QIcon("myIcon.ico"))
self.setMaximumSize(width, height)
self.setMinimumSize(1000, 1000)
self.canvas = QGroupBox(self)
self.canvas.setStyleSheet("QGroupBox { border: 1px solid #9F9B9B}")
self.canvas.move(350, 30)
self.canvas.resize(210, 220)
self.bImage = QPushButton("Load Image", self)
self.bImage.move(150, 207)
self.bImage.clicked.connect(self.openImage)
self.show()
def openImage(self):
self.label = QLabel(self)
self.preview = QPixmap("image.png")
self.label.setPixmap(self.preview)
self.label.move(350, 30)
But Strangely if I place the code from the openImage() function into the first lines of the init() funtion the image will be completely displayed.
What shall I do to load the entire image by the openImage() function?
It is generally a bad idea to try to position widgets using absolute values. You should always use layouts whenever possible. The reason why the image doesn't show is because you move the label to a position behind the group-box. Instead, you should put the label in a layout inside the group-box:
class MyWindow(QtGui.QWidget):
def __init__(self):
...
self.canvas = QtGui.QGroupBox(self)
...
self.label = QtGui.QLabel(self)
layout = QtGui.QVBoxLayout(self.canvas)
layout.addWidget(self.label)
...
def openImage(self):
self.preview = QtGui.QPixmap("image.png")
self.label.setPixmap(self.preview)
Right now I have a GUI built from wxpython that successfully pulls data from a .csv, populates a wx grid object, and then displays it in a new frame. I have also successfully got my main window to display some information in the notebook style. My goal is to make it so that when I run my program, one of the main page tabs contains the same populated grid as the window I previously made. The issue that keeps stumping me is that the grid creation and grid population (two separate things) are done in different classes in a different (but imported) local file. Additionally, the below code in my program context gives the AttributeError: 'TabPanel' object has no attribute 'con', which makes sense, but I don't
Is this not possible or is there something I am missing (and am I even being clear?)? Below is what I would guess would be relevant code. (Incorrect spacing for class and constructor lines just for convenience here.) Thank you very much!
Tab/Notebook:
class TabPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.Panel(Employee.EmployeeViewAllFrame(self).show())
self.sizer.Add(txtOne, 0, wx.ALL , 50)
self.SetSizer(self.sizer)
class NotebookDemo(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=
wx.BK_DEFAULT
#wx.BK_TOP
#wx.BK_BOTTOM
#wx.BK_LEFT
#wx.BK_RIGHT
)
# Create the first tab and add it to the notebook
tabOne = TabPanel(self)
tabOne.SetBackgroundColour("BLUE")
self.AddPage(tabOne, "Main")
# Create and add the second tab
tabTwo = TabPanel(self)
self.AddPage(tabTwo, "Employees")
# Create and add the third tab
self.AddPage(TabPanel(self), "Tasks")
Grid/Frame:
class empGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self,parent,size = (1500,1000))
self.SetDefaultCellOverflow(False)
self.EnableEditing(False)
self.EnableDragGridSize(False)
self.EnableDragRowSize(False)
self.EnableDragColSize(False)
self.grid = gridlib.Grid(panel2)
self.CreateGrid(TOTALEMPLOYEES, 12)
self.SetColLabelValue(0, "Name")
self.SetColSize(0, 200)
self.SetColLabelValue(1, "Grade")
self.SetColLabelValue(2, "NGID")
self.SetColLabelValue(3, "MyID")
self.SetColLabelValue(4, "Skillset1")
self.SetColSize(4, 110)
self.SetColLabelValue(5, "Skillset2")
self.SetColSize(5, 110)
self.SetColLabelValue(6, "SME")
self.SetColLabelValue(7, "Org")
self.SetColLabelValue(8, "Manager")
self.SetColSize(8, 125)
self.SetColLabelValue(9, "OfficePriority")
self.SetColSize(9, 165)
self.SetColLabelValue(10, "Comments")
self.SetColSize(10, 200)
self.SetColLabelValue(11, "Loan?")
#self.AutoSizeColumns(setAsMin=True)
class EmployeeViewAllFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent,title = 'View All Employees',size=(wx.EXPAND,wx.EXPAND))
self.currentid = ''
self.currentrow = 0
self.parent = parent
#Declare all panels
self.panelMain = wx.Panel(self,size = (1500, 1000))
self.panelSide = wx.Panel(self,size = (wx.EXPAND, 1000))
#self.panelTitle = wx.Panel(self,size = (1000,30))
#self.buttonPanel = wx.Panel(self)
self.buttonExit = wx.Button(self.panelSide, label="exit")
self.buttonExit.Bind(wx.EVT_BUTTON, self.OnExitButton)
cur = self.parent.con.cursor()
cur.execute("SELECT * FROM " + EMPLOYEETABLE + " ORDER BY Name;")
self.rows = cur.fetchall()#Load all the employees into self.rows and organize by name
self.employeenumber = len(self.rows) #Going to be the fetched number from the database
global TOTALEMPLOYEES
TOTALEMPLOYEES = self.employeenumber
#Set up all the column panels and place into an array to be modified
#self.empGrid = empGrid(self.panelMain)
self.empGrid = empGrid(EMP.MainWindow.panel2)
for i in xrange (0, TOTALEMPLOYEES):
self.empGrid.SetRowLabelValue(i, str(i+1))
for j in xrange (0,12):
self.empGrid.SetCellValue(i, j, str(self.rows[i][j]))
if i % 2 == 1:#if it is odd, change the color to make it easier on the eyes
self.empGrid.SetCellBackgroundColour(i, j, 'LIGHT BLUE') #JTEST
self.empGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.OnGridDoubleClick)
self.empGrid.Bind(wx.grid.EVT_GRID_CELL_RIGHT_DCLICK, self.OnGridDoubleClickRight)
#Now do the same thing for the buttons
text = wx.StaticText(self.panelSide, label = "Double left click an employee to modify fields\n\n\n Double right click an employee to add a new employee task" , size = (wx.EXPAND,400))
sideSizer = wx.BoxSizer(wx.VERTICAL)
sideSizer.Add(text)
sideSizer.Add(self.buttonExit)
self.panelSide.SetSizer(sideSizer)
self.panelSide.Layout()
#Put them all together then display
displayEmployeeSizer = wx.BoxSizer(wx.VERTICAL)
displayEmployeeSizer.Add(self.empGrid) #JGRID
self.panelMain.SetSizer(displayEmployeeSizer)
self.panelMain.Layout()
viewEmployeeSizer = wx.BoxSizer(wx.HORIZONTAL)
#viewEmployeeSizer.Add(self.panelTitle,proportion=0)
viewEmployeeSizer.Add(self.panelMain,proportion=0)
viewEmployeeSizer.Add(self.panelSide,proportion = 0)
#viewEmployeeSizer.Add(self.buttonPanel, proportion=0, flag = wx.ALIGN_CENTER_HORIZONTAL)
#viewEmployeeSizer.Add(self.buttonExit, proportion = 0, flag = wx.ALIGN_CENTER_HORIZONTAL)
self.SetSizer(viewEmployeeSizer) #Set the panel size
#self.buttonPanel.Layout()
self.Layout()
self.Show()
You can't show the exact same widget in two different parents. Instead, you will need to create an instance of the empGrid when you create your standalone frame AND a different instance when you create the notebook.
When you instantiate the empGrid, you pass it the the notebook panel/page as its parent. When you create the frame, you will pass the frame (or its panel) as the parent.
I want to make a game in wxPython (no other modules) and I want to make it so that you can enter some values in popup screens before the game starts, and then the game will be drawn on a canvas which in turn is drawn on a panel, which is bound to the main game.
I made the gamescreen with all fancy stuff (works solo)
I made the input screens
But I cannot link them.
How do I start the game so it will open a dialog box, then on the closure of it open another one, and then open the game ?
I tried the following, but it will not open my canvas:
# makes a game by showing 2 dialogs
# after dialogs have been answered, starts the game by drawing the canvas.
# imports
import wx
import Speelveld3
# globals
SCRWIDTH = 950
SCRHEIGHT = 700
# dialogbox class
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
class MyDialog2(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.canvasWidth = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
# main class
class Game(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, title='My game', size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.canvasWidth = 10
# hide the frame for now
self.Hide()
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg1.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.username = dlg1.username.GetValue()
#clean up the dialog (AFTER you get the username)
dlg1.Destroy()
dlg2 = MyDialog2(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg2.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.canvasWidth = dlg2.canvasWidth.GetValue()
#clean up the dialog (AFTER you get the username)
dlg2.Destroy()
# Now that you have your settings, Make the gameboard
# THIS PART IS STILL BROKEN!
# I can paste the whole board class (structure of it is taken from the tetris tutorial)
# but that seems a bit much tbh...
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
if __name__ == '__main__':
# how can I start the game here?
app = wx.App()
frame = Game()
board = Speelveld3.Speelveld(frame)
board.start()
frame.Show()
app.MainLoop()
You've double posted, and the lack of any wx.Dialog in your sample code suggests to me that you haven't even looked at a tutorial yet, but I will give you the benefit of the doubt.
First, if you want to return information from a dialog, the easiest way is to define a custom dialog. Define a new class that inherits from wx.Dialog and then set it up just like you would a normal panel or a frame. It seems to me that you will need two of these. They'll look something like this:
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self) #this is where users will enter their username
self.okButton = wx.Button(self, wx.ID_OK, "OK") #Note that I'm using wx.ID_OK. This is important
Now, for the logic you want. Pretty much every object in wxPython that you actually see has the functions Show() and Hide() (API here). You don't want to show your frame until AFTER the dialogs are finished, so in your __init__(), call Hide(). I'm also initializing a variable, username, which is where I will store the data from my dialog.
class Game(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.Hide() #don't show the frame just yet
#self.Hide() is the exact same as self.Show(False)
Now, for your dialogs. Like Mike Driscoll suggested, you call your dialogs BEFORE making your canvas. wx.Dialogs are launched using ShowModal(). By setting the ID of self.okButton to the constant wx.ID_OK, wxPython recognizes that the dialog should be closed after the button in clicked. You should also be aware of wx.ID_CANCEL.
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
if dlg1.ShowModal() == wx.ID_OK:
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
self.username = dlg1.username.GetValue() #get the username from the dialog
dlg1.Destroy() #clean up the dialog (AFTER you get the username)
#do this again for your second dialog
#Now that you have your settings, Make the gameboard
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
In your OnInit you just need to call your dialogs and show them modally BEFORE you create your Board instance. Then it should work correctly.
EDIT (6-28-12): Here's some code:
import wx
########################################################################
class MyDlg(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="I'm a dialog!")
lbl = wx.StaticText(self, label="Hi from the panel's init!")
btn = wx.Button(self, id=wx.ID_OK, label="Close me")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lbl, 0, wx.ALL, 5)
sizer.Add(btn, 0, wx.ALL, 5)
self.SetSizer(sizer)
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
# show a custom dialog
dlg = MyDlg()
dlg.ShowModal()
dlg.Destroy()
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
pdc = wx.PaintDC(self)
try:
dc = wx.GCDC(pdc)
except:
dc = pdc
rect = wx.Rect(0,0, 100, 100)
for RGB, pos in [((178, 34, 34), ( 50, 90)),
(( 35, 142, 35), (110, 150)),
(( 0, 0, 139), (170, 90))
]:
r, g, b = RGB
penclr = wx.Colour(r, g, b, wx.ALPHA_OPAQUE)
brushclr = wx.Colour(r, g, b, 128) # half transparent
dc.SetPen(wx.Pen(penclr))
dc.SetBrush(wx.Brush(brushclr))
rect.SetPosition(pos)
dc.DrawRoundedRectangleRect(rect, 8)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Example frame")
# show a MessageDialog
style = wx.OK|wx.ICON_INFORMATION
dlg = wx.MessageDialog(parent=None,
message="Hello from the frame's init",
caption="Information", style=style)
dlg.ShowModal()
dlg.Destroy()
# create panel
panel = MyPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()
Using PyQt4.
My goal is to load in "parts" of a .png, assign them to QGraphicsItems, add them to the scene, and have the QGraphicsView display them. (Right now I don't care about their coordinates, all I care about is getting the darn thing to work).
Currently nothing is displayed. At first I thought it was a problem with items being added and QGraphicsView not updating, but after reading up a bit more on viewports, that didn't really make sense. So I tested adding the QGraphicsView items before even setting the view (so I know it wouldn't be an update problem) and it still displayed nothing. The path is definitely correct. Here is some code that shows what is going on...
Ignore spacing issues, layout got messed up when pasting
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
self.setWindowTitle('NT State Editor')
winWidth = 1024
winHeight = 768
screen = QtGui.QDesktopWidget().availableGeometry()
screenCenterX = (screen.width() - winWidth) / 2
screenCenterY = (screen.height() - winHeight) / 2
self.setGeometry(screenCenterX, screenCenterY, winWidth, winHeight)
self.tileMap = tilemap.TileMap()
self.tileBar = tilebar.TileBar()
mapView = QtGui.QGraphicsView(self.tileMap)
tileBarView = QtGui.QGraphicsView(self.tileBar)
button = tilebar.LoadTilesButton()
QtCore.QObject.connect(button, QtCore.SIGNAL('selectedFile'),
self.tileBar.loadTiles)
hbox = QtGui.QHBoxLayout()
hbox.addWidget(mapView)
hbox.addWidget(self.tileBarView)
hbox.addWidget(button)
mainWidget = QtGui.QWidget()
mainWidget.setLayout(hbox)
self.setCentralWidget(mainWidget)
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
--
class Tile(QtGui.QGraphicsPixmapItem):
def __init__(self, parent = None):
QtGui.QGraphicsPixmapItem(self, parent)
self.idAttr = -1
class TileBar(QtGui.QGraphicsScene):
def __init__(self, parent = None):
QtGui.QGraphicsScene.__init__(self, parent)
def loadTiles(self, filename):
tree = ElementTree()
tree.parse(filename)
root = tree.getroot()
sheets = root.findall('sheet')
for sheet in sheets:
sheetPath = sheet.get('path')
sheetImg = QtGui.QImage(sheetPath)
strips = sheet.findall('strip')
for strip in strips:
tile = Tile()
tile.idAttr = strip.get('id')
clip = strip.find('clip')
x = clip.get('x')
y = clip.get('y')
width = clip.get('width')
height = clip.get('height')
subImg = sheetImg.copy(int(x), int(y), int(width), int(height))
pixmap = QtGui.QPixmap.fromImage(subImg)
tile.setPixmap(pixmap)
self.addItem(tile)
I tried some stuff with connecting the TileBar's 'changed()' signal with various 'view' functions, but none of them worked. I've had a bit of trouble finding good examples of ways to use the Graphics View Framework, (most are very very small scale) so let me know if I'm doing it completely wrong.
Any help is appreciated. Thanks.
It's quite hard to tell what's wrong with your code as it's not complete and missing some parts to get it compiled. Though there are couple of places which could potentially cause the problem:
Your Title class constructor; I believe you should be calling the base class constructor there by executing: QtGui.QGraphicsPixmapItem.__init__(self, parent).
Looks like your graphic scene objects are getting constructed in the button's onclick signal. There could be problems with your signal connecting to the proper slot, you should see warnings in the output if there are such problems in your widget.
It looks like you're loading images file names from the xml file, it's quite hard to check if the logic over there is straight but potentially you could have a problem over there too.
Below is simplified version of your code which loads ab image into the Title and adds it to the graphic scene:
import sys
from PyQt4 import QtGui, QtCore
class Tile(QtGui.QGraphicsPixmapItem):
def __init__(self, parent=None):
QtGui.QGraphicsPixmapItem.__init__(self, parent)
self.idAttr = -1
class TileBar(QtGui.QGraphicsScene):
def __init__(self, parent=None):
QtGui.QGraphicsScene.__init__(self, parent)
#def loadTiles(self, filename):
def loadTiles(self):
sheetImg = QtGui.QImage("put_your_file_name_here.png")
pixmap = QtGui.QPixmap.fromImage(sheetImg)
tile = Tile()
tile.setPixmap(pixmap)
self.addItem(tile)
# skipping your ElementTree parsing logic here
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setWindowTitle('NT State Editor')
winWidth = 1024
winHeight = 768
screen = QtGui.QDesktopWidget().availableGeometry()
screenCenterX = (screen.width() - winWidth) / 2
screenCenterY = (screen.height() - winHeight) / 2
self.setGeometry(screenCenterX, screenCenterY, winWidth, winHeight)
#self.tileMap = Tiletilebar.Map()
self.tileBar = TileBar()
#mapView = QtGui.QGraphicsView(self.tileMap)
self.tileBarView = QtGui.QGraphicsView(self.tileBar)
#button = self.tilebar.LoadTilesButton()
button = QtGui.QPushButton()
QtCore.QObject.connect(button, QtCore.SIGNAL("clicked()"),
self.tileBar.loadTiles)
#self.tileBar.loadTiles('some_file_name')
hbox = QtGui.QHBoxLayout()
#hbox.addWidget(mapView)
hbox.addWidget(self.tileBarView)
hbox.addWidget(button)
mainWidget = QtGui.QWidget()
mainWidget.setLayout(hbox)
self.setCentralWidget(mainWidget)
app = QtGui.QApplication([])
exm = MainWindow()
exm.show()
app.exec_()
hope this helps, regards