Logo Pigsyy的博客

博客

新博客

2025-09-08 18:23:20 By Pigsyy
import tkinter as tk
import tkinter.simpledialog
import os
import shutil
from tkinterdnd2 import DND_FILES, TkinterDnD

rt = TkinterDnD.Tk ()
rt.title ('WyOJ 数据配置工具')
rt.geometry('200x120')

# def ask_subtask_nr ():
#     n = tk.simpledialog.askinteger (title='Subtask 数量',
#                                     prompt='要配置的 Subtask 数量',
#                                     initialvalue=0)

#     print (f'你想配置 {n} 个 Subtask')
#     return n

def configure_subtask (i):
    def config ():
        win = tk.Toplevel (rt)
        win.title (f'配置 Subtask {i}')

        droparea = tk.Label (win,
                             text='>>> 请将本 Subtask 中的测试点拖到这里 <<<',
                             height=10)
        txt = tk.Text (win)
        txt.pack ()

        def drop (ev):
            fp = ev.data
            s = ""
            cnt = 0
            for c in fp:
                if c == '{':
                    cnt += 1
                elif c == '}':
                    cnt -= 1
                elif c == ' ' and cnt == 0:
                    s += '\n'
                else:
                    s += c
            txt.insert (tk.END, f'{s}\n')

        droparea.drop_target_register (DND_FILES)
        droparea.dnd_bind ('<<Drop>>', drop)
        droparea.pack ()
        txt.pack ()

        def confirm ():
            file_name = txt.get("1.0", tk.END).strip()
            # print(file_name)
            name[i - 1] = file_name
            win.destroy()

        confirm = tk.Button(win, text = '确认', command = confirm).pack()

    return config

buttons = list ()
def create_subtask_buttons (nr):
    temp = tk.Toplevel(rt)
    for i in range (1, nr + 1):
        butt = tk.Button (temp, text=f'配置 Subtask {i}',
                          command=configure_subtask (i))
        butt.place (x=80, y=20 + i * 40, width=60, height=30)
        butt.pack ()
        buttons.append (butt)
    def damage():
        # for s in name:
        #     print(s)
        win = tk.Toplevel(rt)
        txt = tk.Label(win, text = '输入数据存储路径:').pack()
        pth = tk.Entry(win)
        pth.pack()
        def ask():
            path = pth.get()
            win.destroy()
            print(path)
            idx_in = 0
            idx_out = 0
            subtask_end = []
            subtask_score = []
            subtask_num = 0
            for s in name:
                data = s.split('\n')
                for pt in data:
                    if pt[pt.find('.') : len(pt)] == '.in':
                        idx_in += 1
                        shutil.copy(pt, path + '/data' + str(idx_in) + pt[pt.find('.') : len(pt)])
                        # print(pt + '->' + path + '/data' + str(idx_in) + pt[pt.find('.') : len(pt)])
                    else:
                        idx_out += 1
                        shutil.copy(pt, path + '/data' + str(idx_out) + pt[pt.find('.') : len(pt)])
                        # print(pt + '->' + path + '/data' + str(idx_out) + pt[pt.find('.') : len(pt)])
                subtask_end.append(idx_in)
                subtask_num += 1
                subtask_score.append(int(input('子任务 ' + str(subtask_num) + ' 分值:')))
            with open(path + '/problem.conf', 'w') as f:
                f.write('use_builtin_judger on\nuse_builtin_checker wcmp\n')
                f.write('n_tests ' + str(idx_in) + '\n')
                f.write('n_ex_tests 0\nn_sample_tests 0\ninput_pre data\ninput_suf in\noutput_pre data\noutput_suf out\n')
                f.write('time_limit ' + input('时间限制:') + '\n')
                f.write('memory_limit ' + input('空间限制:') + '\n')
                f.write('n_subtasks ' + str(subtask_num) + '\n')
                for i in range(0, len(subtask_end)):
                    f.write('subtask_end_' + str(i + 1) + ' ' + str(subtask_end[i]) + '\n')
                    f.write('subtask_score_' + str(i + 1) + ' ' + str(subtask_score[i]) + '\n')
            rt.destroy()

        butt = tk.Button(win, text = '确认', command = ask).pack()
        temp.destroy()
    confirm = tk.Button(temp, text = '完成', command = damage)
    confirm.place(x=80, y=20 + (nr + 1) * 40, width=60, height=30)
    confirm.pack()

subtask_number = tk.Label(rt, text="子任务数量")
subtask_number.place(x = 0, y = 0)
cnt = tk.Entry(rt, bd = 6)
cnt.place(x = 0, y = 30)

def ask_subtask_number ():
    nr = int(cnt.get())
    global name
    name = list(range(int(nr)))
    create_subtask_buttons (nr)

button = tk.Button(rt, text='确认', command = ask_subtask_number).place(x = 0, y = 80, width = 200, height = 40)

rt.mainloop ()

评论

暂无评论

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。