尘谌 2023-06-01 16:13 采纳率: 66.7%
浏览 20
已结题

为什么在子界面定义的背景图片运行不了

为什么在父界面也就是外部定义的图片背景可以被运行,但子界面定义的背景图片就运行不了了?

img

子界面从def Guser(): 开始

import tkinter as tk
from tkinter import ttk
from tkinter import *
from PIL import ImageTk, Image




#连接数据库
import pymysql
conn = pymysql.connect(host='127.0.0.1',
                       port=3306,
                       user='root',
                       password='mysql',
                       database='xitong',
                       charset='utf8')
cursor = conn.cursor()# 创建游标



#管理员添加商品
show_shop=tk.Tk()
show_shop.title("管理员操作页面")
show_shop.geometry("700x700+400+20")

image = Image.open("I:\软件工程\网上购物系统\photo\樱桃.gif")
background_image = ImageTk.PhotoImage(image)
background_label = tk.Label(show_shop, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

tk.Label(show_shop, text='商品预览表', font=('SimHei', 24)).place(x=270, y=20)
tk.Label(show_shop, text='商品', font=('KaiTi', 15)).place(x=200, y=410)
tk.Label(show_shop, text='价格', font=('KaiTi', 15)).place(x=200, y=460)
tk.Label(show_shop, text='序号', font=('KaiTi', 13)).place(x=200, y=510)
# 商品输入
sname = tk.Entry(show_shop, font=('FangSong', 15), width=12)
sname.place(x=270, y=410)
# 价格输入
price = tk.Entry(show_shop, font=('FangSong', 15), width=12)
price.place(x=270, y=460)
# 序号输入
snum = tk.Entry(show_shop, font=('FangSong', 15), width=12)
snum.place(x=270, y=510)







def Increase():
    shop_name = sname.get()
    shop_price = price.get()
    shop_snum = snum.get()
    sql = "insert into shopping (sname, price,snum) values (%s, %s, %s)"
    cursor.execute(sql, (shop_name, shop_price, shop_snum))
    conn.commit()


def Delete():
    shop_name = sname.get()
    sql="delete from shopping where sname=%s"
    cursor.execute(sql, (shop_name))
    conn.commit()


def Revamp():
    shop_name = sname.get()
    shop_price = price.get()
    shop_snum = snum.get()
    #update 表名 set 列名1=列值1,列名2=列值2,... where 条件;

    sql='update shopping set sname=%s , price=%s  where snum=%s ;'
    cursor.execute(sql, (shop_name, shop_price,shop_snum))
    conn.commit()


# 创建表格
table_head = ('shop name', 'price', 'shop num')
table_main = ttk.Treeview(show_shop, height=13, show='headings', columns=table_head)

# 设置表头
table_main.heading('shop name', text='商品名字')
table_main.heading('price', text='价格')
table_main.heading('shop num', text='序号')
# 设置位置
table_main.place(x=100, y=80)
# 设置文字对齐
table_main.column('shop name', width=200, anchor='center')
table_main.column('price', width=200, anchor='center')
table_main.column('shop num', width=60, anchor='center')


def Select():
    # 清空表格内容
    table_main.delete(*table_main.get_children())

    # 执行查询语句
    sql = "SELECT * FROM shopping"
    cursor.execute(sql)
    results = cursor.fetchall()

    # 将查询结果插入表格
    for row in results:
        table_main.insert('', 'end', values=row)

def Guser():
    guser=tk.Tk()
    guser.title("用户管理界面")
    guser.geometry('500x600')

    image = Image.open("I:\软件工程\背景图片\三月七.gif")
    background_image = ImageTk.PhotoImage(image)
    background_label = tk.Label(guser, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    # 创建表格
    table_head = ('name', 'password', 'mibao')
    table_main = ttk.Treeview(guser, height=13, show='headings', columns=table_head)

    # 设置表头
    table_main.heading('name', text='用户名')
    table_main.heading('password', text='密码')
    table_main.heading('mibao', text='密保')
    # 设置位置
    table_main.place(x=50, y=70)
    # 设置文字对齐
    table_main.column('name', width=135, anchor='center')
    table_main.column('password', width=135, anchor='center')
    table_main.column('mibao', width=110, anchor='center')

    tk.Label(guser, text='注册用户表', font=('SimHei', 24)).place(x=170, y=20)
    tk.Label(guser,text='用户名',font=('KaiTi', 15)).place(x=120, y=400)
    gu_name = tk.Entry(guser, font=('FangSong', 15), width=12)
    gu_name.place(x=220, y=400)

    def Delete():
        gus_name = gu_name.get()
        sql = "delete from user where name=%s"
        cursor.execute(sql, (gus_name))
        conn.commit()

    def Select():
        # 清空表格内容
        table_main.delete(*table_main.get_children())

        # 执行查询语句
        sql = "SELECT * FROM user"
        cursor.execute(sql)
        results = cursor.fetchall()

        # 将查询结果插入表格
        for row in results:
            table_main.insert('', 'end', values=row)



    butt_del_gu = tk.Button(guser, text='删除', font=('KaiTi', 15), width=8, command=Delete)
    butt_del_gu.place(x=100, y=500)
    butt_sel_gu = tk.Button(guser, text='查看', font=('KaiTi', 15), width=8, command=Select)
    butt_sel_gu.place(x=320, y=500)


    guser.mainloop()



butt_add_sure = tk.Button(show_shop, text='添加', font=('KaiTi', 15), width=8, command=Increase)
butt_add_sure.place(x=100, y=600)
butt_del_sure = tk.Button(show_shop, text='删除', font=('KaiTi', 15), width=8, command=Delete)
butt_del_sure.place(x=250, y=600)
butt_rev_sure = tk.Button(show_shop, text='修改', font=('KaiTi', 15), width=8, command=Revamp)
butt_rev_sure.place(x=400, y=600)
butt_sel_sure = tk.Button(show_shop, text='查看', font=('KaiTi', 15), width=8, command=Select)
butt_sel_sure.place(x=550, y=600)
butt_gu_sure = tk.Button(show_shop, text='管理用户', font=('KaiTi', 15), width=8, command=Guser)
butt_gu_sure.place(x=480, y=460)


show_shop.mainloop()

  • 写回答

2条回答 默认 最新

  • 瞬间的未来式 2023-06-02 09:22
    关注

    tk不能新建 guser = tk.Toplevel(show_shop)

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

报告相同问题?

问题事件

  • 系统已结题 6月10日
  • 已采纳回答 6月2日
  • 创建了问题 6月1日

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制