-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminigrid_GUI.py
186 lines (162 loc) · 5.87 KB
/
minigrid_GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QLabel, QPushButton, QHBoxLayout
from PyQt5.QtGui import QIcon, QPixmap, QMovie
from PyQt5.QtCore import QByteArray, QTimer
# from Ui_mainWindow import Ui_MainWindow
# from Ui_minigrid import Ui_MainWindow
from Ui_newGame import Ui_new_game_Dialog
# from Ui_main_scrollbar import Ui_MainWindow
from Ui_scrollbar_v2 import Ui_MainWindow
# class MainWindow_(QMainWindow):
# def __init__(self):
# super().__init__()
#
# self.ui = Ui_MainWindow()
# self.ui.setupUi(self)
# pixmap = QPixmap('games/MiniGrid-Empty-6x6-v0/2019-12-11_13:40:14/game1.png')
#
# self.ui.image_1.setPixmap(pixmap)
# self.ui.image_2.setPixmap(pixmap)
# horiz_l1 = QHBoxLayout()
# label1 = QLabel('label1')
# b1 = QPushButton('b1')
# horiz_l1.addWidget(label1)
# horiz_l1.addWidget(b1)
#
# self.ui.verticalLayout.addLayout(horiz_l1)
# # connect button New Game
# self.new_game_Dialog = NewGame()
# # self.num_games = 2
# # self.ui.new_game_button.clicked.connect(lambda: self.new_game_Dialog.exec_())
# self.ui.new_game_button.clicked.connect(lambda: self.add_row('row1'))
#
# def add_row(self, name):
# horiz = QHBoxLayout()
# label = QLabel(name)
# info_button = QPushButton('info')
# move_button = QPushButton('->')
# horiz.addWidget(label)
# horiz.addWidget(info_button)
# horiz.addWidget(move_button)
# self.ui.verticalLayout_games.addLayout(horiz)
# class MainWindow(QMainWindow):
# def __init__(self):
# super().__init__()
#
# self.ui = Ui_MainWindow()
# self.ui.setupUi(self)
# self.games_items = []
# self.ranking_items = []
# # for each game create a row and add to the tree
#
# label = QLabel()
# label.setText('game 1')
# button = QPushButton()
# button.setText('info')
# image_1 = [label, 'image1', button, 'move ->']
# strings = ['ciao', 'dadas', 'dddd']
# l = [] # list of QTreeWidgetItem to add
# for i in strings:
# l.append(QTreeWidgetItem([i])) # create QTreeWidgetItem's and append them
# # tree.addTopLevelItems(l)
#
# self.ui.tree_games.addTopLevelItems(QTreeWidgetItem(image_1))
#
# # item = QTreeWidgetItem()
# self.ui.tree_games.addTopLevelItems(l)
#
path_of_image = 'games/MiniGrid-Empty-6x6-v0/2019-12-11_13:40:14/game'
games_path = 'games'
env = 'MiniGrid-Empty-6x6-v0'
# os.path.join(folder, 'game.json')
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
print(len(os.listdir(os.path.join(games_path, env))))
# movie = QMovie('image/example.gif', QByteArray())
# self.ui.image_1.setMovie(movie)
# movie.start()
pixmap = QPixmap('games/MiniGrid-Empty-6x6-v0/2019-12-11_13:40:14/game1.png')
self.ui.image_1.setPixmap(pixmap)
# connect button New Game
# self.ui.new_game_pb.clicked.connect(lambda: self.new_game_Dialog.exec_())
# ly = self.ui.games_verticalLayout
# show sequence of images
# TODO show images in loop
# TODO change image only when mouse is on it
self.initTimer()
self.update_image(self.count)
def initUI(self):
"""
Main window initialization
:return:
"""
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# set window title (env name) TODO pass it as argument
self.setWindowTitle(env)
self.new_game_Dialog = NewGame()
self.ui.new_game_pb.clicked.connect(lambda: self.add_row('row1'))
for traj_idx, traj in enumerate(os.listdir(os.path.join(games_path, env))):
# print(traj)
self.add_row('game '+str(traj_idx))
# list of image labels
# TODO remove later
# images_l = []
# images_l.append(self.ui.image_1)
# images_l.append(self.ui.image_2)
def initTimer(self):
"""
Initialize Timer to show images of trajectories in loop
:return:
"""
self.count = 0
timer = QTimer(self)
timer.timeout.connect(lambda: self.update_image(self.count))
timer.start(200)
def update_image(self, count):
"""
Update images of trajectories
:param count: index of image to show
:return:
"""
image = path_of_image + str(count) + '.png'
pixmap = QPixmap(image)
# print('new image : ' + image + ' ' + str(pixmap.isNull()))
# if not pixmap.isNull():
self.count = self.count + 1
# self.ui.image_1.setPixmap(pixmap)
# self.label.adjustSize()
# self.ui.image_1.resize(pixmap.size())
def add_row(self, name):
"""
Add new row ( new game) to the list of games
:param name: name of the new game
:return:
"""
# TODO extend so that it can be used both for new trajectories and for ranking
horiz = QHBoxLayout()
horiz.addWidget(QLabel(name))
count = 0
# pixmap = QPixmap(path_of_image + '0' + '.png')
pixmap = QPixmap('games/MiniGrid-Empty-6x6-v0/2019-12-11_13:40:14/game1.png')
# print(path_of_image + '0' + '.png')
label = QLabel()
label.setPixmap(pixmap)
horiz.addWidget(label)
horiz.addWidget(QPushButton('info'))
horiz.addWidget(QPushButton('->'))
self.ui.verticalLayout_2.addLayout(horiz)
# self.update_image(0)
class NewGame(QDialog):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.ui = Ui_new_game_Dialog()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())