#python #python-3.x #tkinter
#питон #python-3.x #tkinter
Вопрос:
вот код для моего основного файла функция градиента взята из файла фона, код для определения полей ввода отмечен полями # ввода для моих значений с обеих сторон. Мне нужны поля ввода для ввода, а затем, когда нажимаются вертикальные/горизонтальные кнопки, рисуется градиент(мой код градиента уже написан в функции и работает правильно).
from ECGUI import * from backrounds import * from stamps import * mode = "line" last_x = 0 last_y = 0 clicked = False def draw(event, panel): global last_x global last_y if clicked and mode == "line": x = event.x y = event.y panel.draw_line(x, y, last_x, last_y) # reset the last postion to the position we just moved to # so that next time the line will be drawn back here not to # our starting point last_x = x last_y = y def click_down(event, panel): global last_x global last_y global clicked clicked = True last_x = event.x last_y = event.y if mode == "snail": snail(panel,event.x-30,event.y-30,1,1) elif mode == "turtle": turtle(panel,event.x-50,event.y-50,1,1) elif mode == "rabbit": rabbit(panel,event.x-50,event.y-50, 1, 1) elif mode == "cat": cat(panel,event.x-50,event.y-50, 1, 1) elif mode == "rat": rat(panel,event.x-100,event.y-100, 1, 1) # takes an event as a parameter and records that the user is no longer # pressing the mouse down in the clicked variable def click_up(event): global clicked clicked = False # sets our mode to bird so that next time the user clicks on the canvas # a bird will be drawn def snail_helper(): global mode mode = "snail" def rabbit_helper(): global mode mode = "rabbit" # sets our mode to line so that next time the user clicks on the canvas # a line will be drawn def line_helper(): global mode mode = "line" # sets our mode to turtle so that next time the user clicks on the canvas # a turtle will be drawn def turtle_helper(): global mode mode = "turtle" def cat_helper(): global mode mode = "cat" def rat_helper(): global mode mode = "rat" def draw_vert(red_start,green_start,blue_start,red_stop,green_stop,blue_stop,): print("vert") def draw_horz(): gradient(panel,red_start,green_start,blue_start,red_stop,green_stop,blue_stop,"up") def main(): # creates a window with a title and button in a top row and a drawing area window = make_window("A Painters Canvas", "white") # needed so we have the label and button in a row and the canvas below animals_alignment = "left" color_start_alignment = "left" back_alignment = "left" top_frame = add_frame(window,"white",0,0,0,0,0,'y') frame_stamps = add_frame(window,"white",0,0,0,0,0,'y') frame_back = add_frame(window,"white",0,0,0,0,0,'y') add_label(frame_stamps, "Stamps:", side="top",font_family='TimesNewRoman',bold = True) snail_button = add_button( frame_stamps, "snail", side=animals_alignment) turtle_button = add_button( frame_stamps, "turtle",side=animals_alignment) rabbit_button = add_button( frame_stamps, "rabbit",side=animals_alignment) cat_button = add_button( frame_stamps, "cat",side=animals_alignment) rat_button = add_button( frame_stamps, "rat",side=animals_alignment) line_button = add_button( frame_stamps, "pen mode",side=animals_alignment ) add_label(frame_back, "Backrounds:", side="bottom",font_family='TimesNewRoman',bold = True) solid_button = add_button( frame_back,"solid",side=back_alignment) vert_button = add_button( frame_back, "vertical gradient",side=back_alignment) horz_button = add_button( frame_back, "horizontal gradient",side=back_alignment) add_label(top_frame, "Starting Colors:", side=color_start_alignment) # entry boxes for my values add_label(top_frame, "Red:", side=color_start_alignment) red_start = add_entry_box(top_frame,10,side = color_start_alignment) add_label(top_frame, "Blue:", side=color_start_alignment) blue_start = add_entry_box(top_frame,10,side = color_start_alignment) add_label(top_frame, "Green:", side=color_start_alignment) green_start = add_entry_box(top_frame,10,side = color_start_alignment) add_label(top_frame, "Stop Colors:", side=color_start_alignment) add_label(top_frame, "Green:", side="left") green_stop = add_entry_box(top_frame,10,side = "left") add_label(top_frame, "Blue:", side="left") blue_stop = add_entry_box(top_frame,10,side = "left") add_label(top_frame, "Red:", side="left") red_stop = add_entry_box(top_frame,10,side = "left") # entry boxes for my values panel = add_canvas(window, 1000, 800, "white") # sets which functions should run when each mouse action occurs panel["move"] = lambda event: draw(event, panel) panel["press"] = lambda event : click_down(event, panel) panel["release"] = click_up snail_button["command"] = snail_helper rabbit_button["command"] = rabbit_helper line_button["command"] = line_helper turtle_button["command"] = turtle_helper cat_button["command"] = cat_helper rat_button["command"] = rat_helper vert_button["command"] = gradient(panel,red_start,green_start,blue_start,red_stop,green_stop,blue_stop,"right") horz_button["command"] = gradient(panel,red_start,green_start,blue_start,red_stop,green_stop,blue_stop,"right") solid_button["command"] = gradient(panel,red_stop,green_stop,blue_stop,red_stop,green_stop,blue_stop,"right") main()
выход: мне кажется, что цветовые линии начала/остановки не получают ввода, и я не могу понять, как заставить это работать.
Комментарии:
1.Для кнопок vert/horz/solid_buttons вы вызываете один
gradient()
раз во время запуска своей программы и присваиваете ее возвращаемое значение (которое, вероятно, отсутствует) в качествеcommand
опции кнопки. Этот параметр должен быть фактической функцией — если вам нужно предварительно указать некоторые параметры, одним из решений является передачаlambda
функции, которая вызывает фактическую функцию с этими параметрами. Вы уже делаете именно это в случаеpanel
событий перемещения/прессы.2. можете ли вы привести мне пример того, как это выглядит? У меня возникли проблемы с введением этого в код