图像和按钮未显示在弹出窗口中

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

我在这个项目上使用了以前的版本,因为最新版本由于未知原因而损坏。

  1. 之前工作得很好,但突然出现编码错误,我决定重新安装 python 和所有模块,但后来我无法打开客户屏幕,按钮也没有出现
import tkinter as tk 
from tkinter import *
from tkinter import messagebox
import tkinter.font as font
from tkinter import ttk
from PIL import ImageTk, Image
 
def CustomerScreen():
    def Page1():
        root = tk.Tk() 
        root.geometry("1000x500")
        root.title("StarFinity Telescope | See the World Through a new Perspective")
        root['background'] = 'white'
        tk.Label(root, text ="Explore By Models",bg="white",fg="black",font = ("Verdana", 20)).pack()
        C = Canvas(root, bg ="white", height = 250, width = 300)
        load=Image.open("model1.png")
        img3=ImageTk.PhotoImage(load)
        label1=tk.Label(root,image=img3)
        label1.image=img3
        label1.place(x=0,y=0)
        label1.pack()
        root.mainloop()
    
        
        
    def Page2():
        root = tk.Tk() 
        root.geometry("1000x500")
        root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
        root['background'] = '#F8F6F0'
        tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
        
        
    def Page3():
        root = tk.Tk() 
        root.geometry("1000x500")
        root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
        root['background'] = '#F8F6F0'
        tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
        
    def Page4():
        root = tk.Tk() 
        root.geometry("1000x500")
        root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
        root['background'] = '#F8F6F0'
        tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
    
    def Page5():
        root = tk.Tk() 
        root.geometry("1000x500")
        root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
        root['background'] = '#F8F6F0'
        tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
        
       
    root = tk.Tk() 
    root.geometry("1000x562") 
    root.title("Login Page")
    root.configure(bg="white")
    C = Canvas(root, bg ="white", height = 250, width = 300)
    img1=ImageTk.PhotoImage(Image.open("mountainmorning.jpg"))
    label=Label(root,image=img1)
    label.place(x=0,y=0)
    btnpg1 = tk.Button(root, text ="Page 1",fg ='white',bg= 'black',font=("Times New Roman Bold", 10),command = Page1) 
    btnpg1.place(x = 1075, y = 250, width = 60)
    btnpg2 = tk.Button(root, text ="Page 2",fg ='white',bg= 'black',font=("Times New Roman Bold", 10),command = Page2) 
    btnpg2.place(x = 1075, y = 300, width = 60)
    btnpg3 = tk.Button(root, text ="Page 3",fg ='white',bg= 'black',font=("Times New Roman Bold", 10),command = Page5) 
    btnpg3.place(x = 1075, y = 350, width = 60)
    btnpg4 = tk.Button(root, text ="Page 4",fg ='white',bg= 'black',font=("Times New Roman Bold", 10),command = Page5) 
    btnpg4.place(x = 1075, y = 400, width = 60)
    btnpg5 = tk.Button(root, text ="Page 4",fg ='white',bg= 'black',font=("Times New Roman Bold", 10),command = Page5) 
    btnpg5.place(x = 1075, y = 450, width = 60)
    lblname = tk.Label(root, text ="The art of engineering.",bg= 'black',fg ='white',font=("Times New Roman Bold", 18) ) 
    lblname.place(x = 400, y = 470)
    
    
    root.mainloop()
def loginscreen():
    def clear():
        txtUser.delete(0,END)
        txtpass.delete(0,END)
        txtUser.focus()

    def login():     
        user = txtUser.get() 
        password = txtpass.get()
        if user in alog.keys():
            if password ==alog[user]:
                messagebox.showinfo("Admin Login", "Login Successful. Welcome Back, Sir!")
                root.destroy() 
                AdminScreen()
            else:
                messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
        
        
        elif user in log.keys():
            if password==log[user]:
                messagebox.showinfo("Login ", "Login Successful. Welcome Back!")
                root.destroy()
                CustomerScreen()
                
            else:
                messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
        else:
            messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
    def signup():
        user=txtUser.get()
        password=txtpass.get()
        if user in log.keys():
            messagebox.showinfo("","this account already exists. please login to your account")
        else:
            op1=messagebox.askquestion("","Are you sure that you want this to be your LoginID and Password?")
            if op1 =="yes":
                op2=messagebox.askquestion("","Are you a 101% sure?")
                if  op2=="yes":
                    log[user]=password      
    from PIL import ImageTk,Image    
    root = tk.Tk() 
    root.geometry("1000x565") 
    root.title("Login Page")
    root.configure(bg="white")
    C = Canvas(root, bg ="white", height = 250, width = 300)
    img3=ImageTk.PhotoImage(Image.open("image.jpg"))
    label=Label(root,image=img3)
    label.place(x=0,y=-10)
    
    lbllogin = tk.Label(root, text ="LoginID",fg="Black",bg="white",font=("Times New Roman Bold", 18) ) 
    lbllogin.place(x = 680, y = 250) 
    txtUser = tk.Entry(root, width = 35,fg="Black",bg="white",font=("Times New Roman Bold", 10)) 
    txtUser.place(x = 830 , y = 250, width = 150) 
    lblpass = tk.Label(root, text ="Password",fg="Black",bg="white",font=("Times New Roman Bold", 18)) 
    lblpass.place(x = 680 , y = 325) 
    txtpass = tk.Entry(root,show="*", width = 35,fg="blue",bg="white",font=("Times New Roman Bold", 10)) 
    txtpass.place(x = 830, y = 325, width = 150) 
    loginbtn = tk.Button(root, text ="Login", fg ='black', font=("Times New Roman Bold", 18),command = login) 
    loginbtn.place(x = 690, y = 450, width = 100)
    clearbtn = tk.Button(root, text ="Clear",fg ='black', font=("Times New Roman Bold", 18),command = clear) 
    clearbtn.place(x = 890, y = 450, width = 100)
    signupbtn = tk.Button(root, text ="Sign Up",fg ='black',font=("Times New Roman Bold", 18),command=signup) 
    signupbtn.place(x = 790, y = 450, width = 100)
    root.mainloop()


我得到的错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ypgnv\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "C:\Users\ypgnv\Desktop\telescope\telescope.py", line 340, in login
    CustomerScreen()
  File "C:\Users\ypgnv\Desktop\telescope\telescope.py", line 120, in CustomerScreen
    img1=ImageTk.PhotoImage(Image.open("mountainmorning.jpg"))
  File "C:\Users\ypgnv\AppData\Local\Programs\Thonny\lib\site-packages\PIL\ImageTk.py", line 129, in __init__
    self.paste(image)
  File "C:\Users\ypgnv\AppData\Local\Programs\Thonny\lib\site-packages\PIL\ImageTk.py", line 175, in paste
    im.load()
  File "C:\Users\ypgnv\AppData\Local\Programs\Thonny\lib\site-packages\PIL\ImageFile.py", line 312, in load
    raise _get_oserror(err_code, encoder=False)
OSError: broken data stream when reading image file

我决定重新安装 python 和所有模块,但后来我无法打开客户屏幕,按钮也没有出现

python python-3.x tkinter tkinter-button thonny
1个回答
0
投票

图像和按钮未显示在弹出窗口中

您的 x=1075,布局超出了窗口。因此,对于按钮小部件,每 30 步设置 x=15 和 y=320。

问题可以解决。

  • 第8行,注释掉

    #def CustomerScreen()

  • 缩进第1页、第2页、第3页、第4页和第5页

  • 改变这个

    root = tk.root()

    至:

    root = tk.Toplevel()
    

    适用于第 1 页、第 2 页、第 3 页、第 4 页和第 5 页上的每个功能

  • 第 23 行,注释掉

    #root.mainloop()

  • 第56行,注释掉

    #from PIL import ImageTk,Image

  • 最后,为按钮小部件每 30 步更改 x=15 和 y=320 和布局。

这是代码:

import tkinter as tk 
from tkinter import *
from tkinter import messagebox
import tkinter.font as font
#from tkinter import ttk
from PIL import ImageTk, Image
 
#def CustomerScreen():
             
def Page1():
    root = tk.Toplevel() 
    root.geometry("1000x500")
    root.title("StarFinity Telescope | See the World Through a new Perspective")
    root['background'] = 'white'
    tk.Label(root, text ="Explore By Models",bg="white",fg="black",font = ("Verdana", 20)).pack()
    C = Canvas(root, bg ="white", height = 250, width = 300)
    load=Image.open("p6.png")
    img3=ImageTk.PhotoImage(load)
    label1=tk.Label(root,image=img3)
    label1.image=img3
    label1.place(x=0,y=0)
    label1.pack()
     

    
    
def Page2():
    root = tk.Toplevel() 
    root.geometry("1000x500")
    root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
    root['background'] = '#F8F6F0'
    tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
    
    
def Page3():
    root = tk.Toplevel() 
    root.geometry("1000x500")
    root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
    root['background'] = '#F8F6F0'
    tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
    
def Page4():
    root = tk.Toplevel() 
    root.geometry("1000x500")
    root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
    root['background'] = '#F8F6F0'
    tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()

def Page5():
    root = tk.Toplevel() 
    root.geometry("1000x500")
    root.title("Luxury Sedans, SUVs, Electric Vehicles & More | Audi Dubai | Audi Middle East")
    root['background'] = '#F8F6F0'
    tk.Label(root, text ="LIST OF VEHICLES",bg="#F8F6F0",fg="#e82127",font = ("OCR A Extended", 20)).pack()
        
#from PIL import ImageTk,Image        
root = tk.Tk() 
root.geometry("1000x562") 
root.title("Login Page")
root.configure(bg="white")
C = Canvas(root, bg ="white", height = 250, width = 300)
img1=ImageTk.PhotoImage(Image.open("p1.png"))
label=Label(root,image=img1)
label.place(x=0,y=0)

#pg = CustomerScreen()
btnpg1 = tk.Button(root, text ="Page 1",fg ='white',bg= 'black',font=("Times New Roman Bold", 10), command=Page1) 
btnpg1.place(x = 15, y = 320, width = 60)
btnpg2 = tk.Button(root, text ="Page 2",fg ='white',bg= 'black',font=("Times New Roman Bold", 10), command=Page2) 
btnpg2.place(x = 15, y = 350, width = 60)
btnpg3 = tk.Button(root, text ="Page 3",fg ='white',bg= 'black',font=("Times New Roman Bold", 10), command=Page3) 
btnpg3.place(x = 15, y = 380, width = 60)
btnpg4 = tk.Button(root, text ="Page 4",fg ='white',bg= 'black',font=("Times New Roman Bold", 10), command=Page4) 
btnpg4.place(x = 15, y = 410, width = 60)
btnpg5 = tk.Button(root, text ="Page 4",fg ='white',bg= 'black',font=("Times New Roman Bold", 10), command=Page5) 
btnpg5.place(x = 15, y = 440, width = 60)
lblname = tk.Label(root, text ="The art of engineering.",bg= 'black',fg ='white',font=("Times New Roman Bold", 18) ) 
lblname.place(x = 400, y = 470)
    
    
     
def loginscreen():
    def clear():
        txtUser.delete(0,END)
        txtpass.delete(0,END)
        txtUser.focus()

    def login():     
        user = txtUser.get() 
        password = txtpass.get()
        if user in alog.keys():
            if password ==alog[user]:
                messagebox.showinfo("Admin Login", "Login Successful. Welcome Back, Sir!")
                root.destroy() 
                AdminScreen()
            else:
                messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
        
        
        elif user in log.keys():
            if password==log[user]:
                messagebox.showinfo("Login ", "Login Successful. Welcome Back!")
                root.destroy()
                CustomerScreen()
                
            else:
                messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
        else:
            messagebox.showinfo("Login ", "Login Denied. Please Check Login Details Again.")
    def signup():
        user=txtUser.get()
        password=txtpass.get()
        if user in log.keys():
            messagebox.showinfo("","this account already exists. please login to your account")
        else:
            op1=messagebox.askquestion("","Are you sure that you want this to be your LoginID and Password?")
            if op1 =="yes":
                op2=messagebox.askquestion("","Are you a 101% sure?")
                if  op2=="yes":
                    log[user]=password      
      
    root = tk.Tk() 
    root.geometry("1000x565") 
    root.title("Login Page")
    root.configure(bg="white")
    C = Canvas(root, bg ="white", height = 250, width = 300)
    img3=ImageTk.PhotoImage(Image.open("image.jpg"))
    label=Label(root,image=img3)
    label.place(x=0,y=-10)
    
    lbllogin = tk.Label(root, text ="LoginID",fg="Black",bg="white",font=("Times New Roman Bold", 18) ) 
    lbllogin.place(x = 680, y = 250) 
    txtUser = tk.Entry(root, width = 35,fg="Black",bg="white",font=("Times New Roman Bold", 10)) 
    txtUser.place(x = 830 , y = 250, width = 150) 
    lblpass = tk.Label(root, text ="Password",fg="Black",bg="white",font=("Times New Roman Bold", 18)) 
    lblpass.place(x = 680 , y = 325) 
    txtpass = tk.Entry(root,show="*", width = 35,fg="blue",bg="white",font=("Times New Roman Bold", 10)) 
    txtpass.place(x = 830, y = 325, width = 150) 
    loginbtn = tk.Button(root, text ="Login", fg ='black', font=("Times New Roman Bold", 18),command = login) 
    loginbtn.place(x = 690, y = 450, width = 100)
    clearbtn = tk.Button(root, text ="Clear",fg ='black', font=("Times New Roman Bold", 18),command = clear) 
    clearbtn.place(x = 890, y = 450, width = 100)
    signupbtn = tk.Button(root, text ="Sign Up",fg ='black',font=("Times New Roman Bold", 18),command=signup) 
    signupbtn.place(x = 790, y = 450, width = 100)
root.mainloop()

在屏幕截图中,您可以在右侧看到五 (5) 个顶级:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.