如何将此 Python 脚本转换为可导入的模块?

问题描述 投票:0回答:0

我希望这段代码像一个库一样运行,但我对此感到很尴尬,而且 chatgpt 不明白我想要什么

from tkinter import *
import logging
import os



try:
    with open('errors.log', 'r') as file:
        print('errors found! printing previous errors and deleting older file')
        contents = file.read()
        print(contents)
        


    os.remove('errors.log')
    print('==EOF==\nfrom now all errors happend really and you should get a gui for it aswell')
except FileNotFoundError:
    print("no previous erros found starting app now!")

def log_error(exception):
    
    logging.basicConfig(filename='errors.log', level=logging.ERROR, format='%(asctime)s:%(levelname)s:%(message)s')
    
    
    console = logging.StreamHandler()
    console.setLevel(logging.ERROR)
    formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)

    
    logging.error(str(exception))

isexecuting = False
x = 0
y = 0

def change_y_cos_idk_how():
    global y
    y = 2
    return y


def guiprint(parameter):
    global x, text_field
    text_field.config(state=NORMAL)
    if x == 0: 
        text_field.insert(END, f"{parameter}") 
        x = 1
    else:
        text_field.insert(END, f"\n{parameter}")
    text_field.config(state=DISABLED)

def waitforint():

    global y, window
    userinputentry = Entry(window, font=("Arial", 14), bg="#333", fg="#fff") 
    userinputentry.pack(pady=10)
    userinputbutton = Button(window, text="Submit", command=change_y_cos_idk_how, font=("Arial", 14), bg="#555", fg="#fff", activebackground="#555", activeforeground="#fff")
    userinputbutton.pack(pady=10)
    
    while y == 0:
        window.update()
    y = 0 
    thing = userinputentry.get() 
    userinputbutton.destroy()
    userinputentry.destroy()
    
    try: 
        thing = int(thing)
    except ValueError as e:
        guiprint("Invalid input. Please enter an integer.")
        log_error(e) 
        return waitforint() 
    return thing
def dontrunagain():
    global isexecuting
    if isexecuting == False:
        isexecuting = True
        execution()
        isexecuting = False
    else:
        log_error("user tried clicking on the execute button again")
        guiprint("code is already running")
def execution():
    #basically to make this execution part to run in another file without defining it or anything just to go like guiprint() and waitforint() inside another file like a library
    global text_field
    guiprint('How old are you?')
    age = waitforint()
    if age == 69:
        guiprint("Me too!")
    elif age > 69:
        guiprint("You're older than me.")
    else:
        guiprint("You're younger than me.")


window = Tk()
window.geometry("500x500+700+250")
window.title("Gui Exec")

window.configure(bg="#222")

execute = Button(window, text="Execute Code", command=dontrunagain, font=("Arial", 16), bg="#555", fg="#fff", activebackground="#555", activeforeground="#fff")
execute.pack(pady=20)

text_field = Text(window, height=10, font=("Arial", 14), bg="#333", fg="#fff", state=DISABLED)
text_field.pack(pady=20)

window.mainloop()

这是一个一次性帐户,因为这段代码太糟糕了,但如果有人知道如何修复它或让它运行,它可能会发生堆栈溢出

现在我希望这个函数不再存在,而是导入

def execution():
    #basically to make this execution part to run in another file without defining it or anything just to go like guiprint() and waitforint() inside another file like a library
    global text_field
    guiprint('How old are you?')
    age = waitforint()
    if age == 69:
        guiprint("Me too!")
    elif age > 69:
        guiprint("You're older than me.")
    else:
        guiprint("You're younger than me.")


我想要一个更像这样的文件而不是执行()

import terriblecode
terriblecode.guiprint("this code is terrible")
this_thing_make_me_puke = terriblecode.waitforint()
terriblecode.guiprint(f'wow number {this_thing_make_me_puke}')
tkinter python-import
© www.soinside.com 2019 - 2024. All rights reserved.