Уведомления

Группа в Telegram: @pythonsu

#1 Янв. 9, 2018 17:06:02

Alex_kuhun
Зарегистрирован: 2017-12-14
Сообщения: 2
Репутация: +  0  -
Профиль   Отправить e-mail  

Смена окон в Tkinter

Всем привет. Помогите, пожалуйста, со сменой окон. Идея в том, что в окне выводятся восемь картинок и пользователь с помощью стрелочных клавиш может выбрать необходимую ему картинку (выбранная картинка подсвечивается красным). Нажав на выбранной картинке “плюс” или “минус”, выбранная картинка заменяется соответствующим изображением. Это получилось реализовать. Теперь к делу:
Нужно, чтобы обусловленное выше повторялось несколько раз (например, 4 раза). Т.е. на экран выводится 8 картинок, пользователь обозначил “плюс”, “минус”, окно закрылось, открылось новое и снова то же самое. Пробую через цикл for, внутри while, в котором ждать пока будет выбран “плюс” и “минус”. Но что-то не выходит. Подскажите, как можно ждать в цикле, пока переменная не получит определенное значение? Или не менять окно на новое, а просто обновлять текущее? Код ниже. Спасибо.

from Tkinter import*
import os, sys, random
from PIL import ImageTk, Image

def load_all_images(directory):
images = os.listdir(directory)
random.shuffle(images)
return images

def rightKey(event):
global index
if index != 7:
set.config(bg='red', bd=5)
set.config(bg='black', bd=1)
index = index + 1
current_x = set.winfo_x()
current_y = set.winfo_y()

def leftKey(event):
global index
if index != 0:
set.config(bg='red', bd=5)
set.config(bg='black', bd=1)
index = index - 1
current_x = set.winfo_x()
current_y = set.winfo_y()

def upKey(event):
global index
if index >= 4:
set.config(bg='red', bd=5)
set.config(bg='black', bd=1)
index = index - 4
current_x = set.winfo_x()
current_y = set.winfo_y()

def downKey(event):
global index
if index < 4:
set.config(bg='red', bd=5)
set.config(bg='black', bd=1)
index = index + 4
current_x = set.winfo_x()
current_y = set.winfo_y()

def plusKey(event):
global counter_selection_plus
if counter_selection_plus < 2:
set.config(bg='black', bd=1)
current_x = set.winfo_x()
current_y = set.winfo_y()
selected_plus.append(my_img)
photo = PhotoImage(file='max.gif')
panel = Label(root, image=photo, bg = ‘red’, bd = 5)
panel.photo = photo
panel.pack()
set = panel
panel.place(x=current_x, y=current_y)
counter_selection_plus += 1

def minusKey(event):
global counter_selection_minus
if counter_selection_minus < 2:
set.config(bg='black', bd=1)
current_x = set.winfo_x()
current_y = set.winfo_y()
selected_minus.append(my_img)
photo = PhotoImage(file='min.gif')
panel = Label(root, image=photo, bg = ‘red’, bd = 5)
panel.photo = photo
panel.pack()
set = panel
panel.place(x=current_x, y=current_y)
counter_selection_minus += 1

selected_plus =
selected_minus =

for sheet in range(3):

root = Tk()
root.title(“%i” %sheet)
# set the root window's height, width and x,y position
# x and y are the coordinates of the upper left corner
window_width = root.winfo_screenwidth() - 20
window_height = root.winfo_screenheight() - 80
x = 1
y = 1
# use width x height + x_offset + y_offset (no spaces!)
root.geometry(“%dx%d+%d+%d” % (window_width, window_height, x, y))
# root.minsize(window_width,window_height)
# root.maxsize(window_width,window_height)
panel_width = window_width / 4
panel_height = window_height / 2

pos_x_up = 0
pos_x_down = 0
pos_y = 0
width = 0
height = 0
counter_position = 0

current_x = 0
current_y = 0

counter_selection_plus = 0
counter_selection_minus = 0
my_img = load_all_images('1' )
set =

for name in my_img:
photo = PhotoImage(file='1/%s' %name)
panel = Label(root, image=photo, bg = ‘black’, bd = 1)
panel.photo = photo
panel.pack()
set.append(panel)
if counter_position < 4:
panel.place(x = pos_x_up, y = 0)
pos_x_up = pos_x_up + panel_width
else:
panel.place(x=pos_x_down, y = panel_height-15)
pos_x_down = pos_x_down + panel_width
counter_position += 1

set.config(bg = ‘red’, bd = 5)
start = 0
set.focus_set()
index = start

while counter_selection_plus != 1 or counter_selection_minus != 1:
set.bind(“<Right>”, rightKey)
set.bind(“<Left>”, leftKey)
set.bind(“<Up>”, upKey)
set.bind(“<Down>”, downKey)
set.bind(“<+>”, plusKey)
set.bind(“<minus>”, minusKey)

if (counter_selection_plus == 1) and (counter_selection_minus == 1):
root.destroy()
break

root.mainloop()

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version