wangderderle 2022-12-31 16:30 采纳率: 100%
浏览 11
已结题

python函数内不运行,单独拿出来就运行

def xinxi():
menu.withdraw()
single=tk.Tk()
single.title("个人信息")
single.geometry("600x600")

    name_lable = tk.Label(single,text="姓名:",font=(12))
    name_lable.place(x=150, y=80, width=50, height=20)
    ent_name = tk.StringVar()
    tk.Entry(single,width=20,textvariable=ent_name).place(x=200, y=80, width=150, height=20)
    
    '''ent_name = tk.Entry(single,textvariable = tk.StringVar(),font = 12)
    ent_name.place(x=200, y=80, width=150, height=20)'''


    sex_lable = tk.Label(single,text="性别:",font=(12))
    sex_lable.place(x=150, y=120, width=50, height=20)

    ent_sex = tk.StringVar()
    tk.Entry(single,width=20,textvariable=ent_sex).place(x=200, y=120, width=150, height=20)

    '''ent_sex = tk.Entry(single,textvariable = tk.StringVar(),font = 12)
    ent_sex.place(x=200, y=120, width=150, height=20)'''


    age_lable = tk.Label(single,text="年龄:",font=(12))
    age_lable.place(x=150, y=160, width=50, height=20)

    ent_age = tk.StringVar()
    tk.Entry(single,width=20,textvariable=ent_age).place(x=200, y=160, width=150, height=20)

    '''ent_age = tk.Entry(single,textvariable = tk.StringVar(),font = (12))
    ent_age.place(x=200, y=160, width=150, height=20)'''
    


    height_lable = tk.Label(single,text="身高:",font=(12))
    height_lable.place(x=150, y=200, width=50, height=20)

    ent_in_height = tk.StringVar()
    tk.Entry(single,width=20,textvariable=ent_in_height).place(x=200, y=200, width=150, height=20)

    '''ent_in_height = tk.Entry(single,textvariable = tk.StringVar(),font = (12))
    ent_in_height.place(x=200, y=200, width=150, height=20)'''

    weight_lable = tk.Label(single,text="体重:",font=(12))
    weight_lable.place(x=150, y=240, width=50, height=20)

    ent_in_weight = tk.StringVar()
    tk.Entry(single,width=20,textvariable=ent_in_weight).place(x=200, y=240, width=150, height=20)

    '''ent_in_weight = tk.Entry(single,textvariable = tk.StringVar(),font = (12))
    ent_in_weight.place(x=200, y=240, width=150, height=20)'''
    def out_information():
        in_username = X
        conn = connet()
        cur = conn.cursor()
        sql = 'select * from 用户数据 where username=%s'
        cur.execute(sql,in_username)
        z = cur.fetchall()
        conn.commit()
        print(z)
        ent_name.set(z[0][1])
        ent_sex.set(z[0][2])
        ent_age.set(z[0][3])
        ent_in_height.set(z[0][4])
        ent_in_weight.set(z[0][5])
        

    #个人信息输入板块
    def in_information():
        print(X)
        print(type(X))
        in_username = X
        in_name = ent_name.get()
        in_sex = ent_sex.get()
        in_age = ent_age.get()
        in_height = ent_in_height.get()
        in_weight = ent_in_weight.get()
        conn = connet()
        cur = conn.cursor()
        sql = 'insert into 用户数据(username,nameval,sex,age,height,weight) values("%s","%s","%s","%s","%s","%s")'
        cur.execute(sql%(in_username,in_name,in_sex,in_age,in_height,in_weight))
        conn.commit()
        conn.rollback()
        tk.messagebox.showinfo(title="attention",message="个人信息输入成功")
        menu.deiconify()
        single.destroy()







    btn_info_button = tk.Button(single,text="输入信息",command=in_information)
    btn_info_button.place(x=200,y=350,width=60,height=45)
    btn_inout_button = tk.Button(single,text="输出个人信息",command = out_information)
    btn_inout_button.place(x=400,y=350,width=60,height=45)
    single.mainloop()

为啥,套在函数里面,数据到数据库里只能显示username,其他的几项都没有也不是空,输出既不报错文本框也不显示出来。但是这部分单独拿出来配上包,都可以运行?ps:输入的部分昨天晚上好好的,今天啥也没动就不行了。

  • 写回答

2条回答 默认 最新

  • xxxhugo5 2022-12-31 16:47
    关注

    这是一段 Python 代码,它包含了一个函数 xinxi。这个函数中包含了一些 GUI 相关的代码,使用了 Tkinter 库来创建一个 GUI 窗口和一些 GUI 元素。

    除了创建 GUI 窗口和 GUI 元素之外,这个函数还有一个内部函数 out_information,它会执行一些数据库相关的操作,并将获取到的数据显示在 GUI 窗口的文本框中。

    如果这个函数单独运行,那么它将创建一个 GUI 窗口并显示出来;如果在其他函数中调用了这个函数,则该函数中的代码将被执行,但是不会显示 GUI 窗口。

    你可以在 Python 程序的主函数中调用 xinxi 函数来执行这个函数中的代码,或者在其他地方调用它以使用它的功能。你也可以在 Python 程序的主函数中调用 xinxi 函数,并通过调用 mainloop 方法来显示 GUI 窗口。

    希望这些信息能帮到你!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月8日
  • 已采纳回答 12月31日
  • 创建了问题 12月31日

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行