首页 > 解决方案 > 我正在用 Python 创建一个 gui 程序,但作业没有运行

问题描述

如果看GUI程序训练视频,编码没有错误,但可以执行。但是,完成语句和任务不会出现并且不起作用。如果您能检查代码有什么问题,我将不胜感激。我目前正在使用 MacBook

import os
import tkinter.ttk as ttk
import tkinter.messagebox as msgbox
from tkinter import * 
from tkinter import filedialog
from PIL import Image




root = Tk()
root.title("Instem GUI")
# root.geometry("0") # 가로 * 세로

# 파일추가, Add File
def add_file():
    files = filedialog.askopenfilenames(title="이미지 파일을 선택하세요", \
        filetypes=(("PNG 파일", "*.png"), ("모든 파일", "*.*")), \
        initialdir="://Users/king/Desktop/practica")
        # 최초에 사용자가 지정하누 경로를 열어줌
    # 선택된 파일 목록
    for file in files:
        list_file.insert(END, file)

# 선택 삭제, Delete Selection
def del_file():
    for index in reversed(list_file.curselection()):
        list_file.delete(index)

# 저장 경로(폴더), Storage Path (Folder)
def browse_dest_path():
    folder_selected = filedialog.askdirectory()
    if folder_selected is None: # 사용자가 취소를 누를 때
        return
   # print(folder_selected)
    txt_dest_path.delete(0, END)
    txt_dest_path.insert(0, folder_selected)
    
# 이미지 통합, Image integration
def merge_image():
   # print(list_file.get(0, END)) # 모든 파일 목록을 가져오기
    images = [Image.open(x) for x in list_file.get(0, END)]
    # size -> size[0] : width, size[1] : height
    widths = [x.size[0] for x in images]
    heights = [x.size[1] for x in  images]
    
    max_width, total_height = max(widths), sum(heights)
    
    # 스케치북 준비, Preparing a Sketchbook
    result_img = Image.new("RGB", (max_width, total_height), (255,255,255))
    y_offset = 0
    for img in images:
        result_img.paste(img, (0, y_offset))
        y_offset += img.size[1]

    dest_path = os.path.join(txt_dest_psth.get(), "instem_photo.jpg") 
    result_img.save(dest_path)
    msgbox.showinfo("알림", "작업이 완료되었습니다.")



# 시작, start
def start():
    # 각 옵션들 값을 확인, Check the value of each option
    print("가로넓이 : ", cmb_width.get())
    print("간격 : ", cmb_space.get())
    print("포맷 : ", cmb_format.get())

    #  파일 목록 확인, Check the file list
    if list_file.size() == 0:
        msgbox.showwarning("경고", "이미지 파일을 추가해주세요")
        return
    # 저장 목록 확인, Check Save List
    if len(txt_dest_path.get()) == 0:
        msgbox.showwarning("경고", "저장 경로를 선택하세요")  
        return

 
# 파일 프레임 파일추가, adding a file frame file
file_frame = Frame(root)
file_frame.pack(fill="x", padx=5, pady=5)

btn_add_file = Button(file_frame, padx=5, pady=5, width=10, text="파일추가", command=add_file)
btn_add_file.pack(side="left")

btn_del_file = Button(file_frame, padx=5, pady=5, width=10, text="선택삭제", command=del_file)
btn_del_file.pack(side="right")

# 리스트 프레임, List Frame
list_frame = Frame(root)
list_frame.pack(fill="both", padx=5, pady=5)

scrollbar = Scrollbar(list_frame)
scrollbar.pack(side="right", fill="y")

list_file = Listbox(list_frame, selectmode="extended", height=15, yscrollcommand=scrollbar.set)
list_file.pack(side="left", fill="both", expand=True)
scrollbar.config(command=list_file.yview)


# 저장 경로  프레임, Storage Path Frame
path_frame = LabelFrame(root, text="저장경로")
path_frame.pack(fill="x", padx=5, pady=5, ipady=5)

txt_dest_path = Entry(path_frame)
txt_dest_path.pack(side="left", fill="x", expand=True, padx=5, pady=5, ipady=4)

btn_dest_path = Button(path_frame, text="찾아보기", width=10, command=browse_dest_path)
btn_dest_path.pack(side="right")

# 옵션 프레임, Option Frame
# 
frame_option = LabelFrame(root, text="Option") 
frame_option.pack(padx=5, pady=5) # Add to Screen

# 가로 넓이, horizontal and horizontal
lbl_width = Label(frame_option, text="가로넓이", width=8) # Put the width in the frame option and zoom in about 3 times the width
lbl_width.pack(side="left") # Add to left screen

# 가로 넓이 콤보, a horizontal combo , Use ttk to use combo 
opt_width = ["원본유지", "1024", "800", "640"] # 
cmb_width = ttk.Combobox(frame_option, state="readonly", values=opt_width, width=10) # Use read-only to receive only values that I specify without receiving the properties specified by the read-only user
cmb_width.current(0) # 첫번째 값을 자동으로 사용 
cmb_width.pack(side="left", padx=5, pady=5)


# 이미지 간격 옵션, Image Interval
lbl_width = Label(frame_option, text="간격", width=8)
lbl_width.pack(side="left")

# 간격 옵션 콤보, Interval Options Combo
opt_space = ["없음", "좁게", "보통", "넓게"]
cmb_space = ttk.Combobox(frame_option, state="readonly", values=opt_space, width=10)
cmb_space.current(0)
cmb_space.pack(side="left", padx=5, pady=5)


# 파일 포맷 옵션, File Format Options
lbl_format = Label(frame_option, text="포맷", width=8)
lbl_width.pack(side="left")

# 파일 포맷 옵션 콤보, File Format Options Combo
opt_format = ["PNG", "JPG", "BMP"]
cmb_format = ttk.Combobox(frame_option, state="readonly", values=opt_format, width=10)
cmb_format.current(0)
cmb_format.pack(side="left", padx=5, pady=5)

# 진행 상황, progress
frame_progress = LabelFrame(root, text="진행상황")
frame_progress.pack(fill="x", padx=5, pady=5)

p_var = DoubleVar()
progress_bar = ttk.Progressbar(frame_progress, maximum=100, variable=p_var)
progress_bar.pack(fill="x", padx=5, pady=5)

# 실행, Press the Start button to start the image integration task
frame_run = Frame(root)
frame_run.pack(fill="x", padx=5, pady=5)
# 닫기 버튼, close btn
btn_close = Button(frame_run, padx=5, pady=5, text="close", width=12, command=root)
btn_close.pack(side="right", padx=5, pady=5)
# 시작 버튼, start btn
btn_start = Button(frame_run, padx=5, pady=5, text="start", width=12, command=start)
btn_start.pack(side="right", padx=5, pady=5)


root.resizable(False, False) # x(너비), y(높이) 값 변경 불가 / 창 크기 조정 설정
 
root.mainloop()

我正在用 Python 创建一个 gui 程序,但作业没有运行

标签: tkinteroperating-systemmacos-catalina

解决方案


推荐阅读