mercredi 15 février 2023

prng,python, tkinter,need to change picture

I ran into this problem and don't know how to solve it. When I press the "generate" button(self.gen), my picture 11 changes to picture 22. BUT I need the image 11 to NOT change when the button is clicked and an error occurs(messagebox.showerror pops up) OR so that after closing the messagebox.showerror, picture 22 changes to 11

import tkinter
from tkinter import *  
from tkinter import messagebox, scrolledtext
from PIL import Image, ImageTk
from random import randint

class App:
    def __init__(self):
        self.window = tkinter.Tk()
        self.window.title("Генератор") 
        self.window['bg'] = '#FFF5EE'
        self.window.geometry('660x550') 
        self.window.resizable(False,False)

        self.lb1 = Label(self.window, text="Enter:", background='#FFF5EE', font = ("Comic Sans MS", 14))  
        self.lb1.grid(column=0, row=2) 

        self.lb2 = Label(self.window, text="min(1-999)",background='#FFF5EE', font = ("Comic Sans MS", 12))  
        self.lb2.grid(column=1, row=3) 

        self.lb3 = Label(self.window, text="max(1-999)", background='#FFF5EE', font = ("Comic Sans MS", 12))  
        self.lb3.grid(column=1, row=4) 

        self.lb4 = Label(self.window, text="amount of numbers", background='#FFF5EE', font = ("Comic Sans MS", 12))  
        self.lb4.grid(column=4, row=3)  

        self.txt2 = Entry(self.window,width=10, borderwidth=3)  
        self.txt2.grid(column=2, row=3)  

        self.txt3 = Entry(self.window,width=10, borderwidth=3)  
        self.txt3.grid(column=2, row=4) 

        self.txt4 = Entry(self.window,width=10, borderwidth=3)  
        self.txt4.grid(column=5, row=3)  

        self.scrolltxt = scrolledtext.ScrolledText(self.window, width=30, height=3, borderwidth=7, state='disabled')
        self.scrolltxt.grid(row=1, column=2, columnspan=3, padx=10, pady=10)

        self.image = Image.open("C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\11.png")
        self.photo = ImageTk.PhotoImage(self.image)

        self.gen = Button(self.window, width = 15, text="Generate", command = lambda:[self.clicked1(), self.gen1()])
        self.gen.grid(row=4, column=6)

        self.canvas = tkinter.Canvas(self.window, height=230, width=230)
        self.canvas.grid(row=0,column=4)
        self.image = self.canvas.create_image(0, 0, anchor='nw', image = self.photo)


        self.btn = Button(self.window, width = 15, text="Delete", command=lambda:[self.delete(),self.clicked2()])  
        self.btn.grid(column=6, row=5)

        self.exit = Button(self.window, width = 15, text="Exit", command=lambda: [self.clicked3(), quit()])
        self.exit.grid(column=6, row=6)

        self.i = Button(self.window, width = 8,text = "i", font = ("Eras Bold ITC", 10) , command = self.inf)
        self.i.grid(row = 0,column = 6)
 
        self.window.mainloop()


    def clicked1(self):
        print("clicked1")
        self.image = Image.open("C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\22.png")  
        self.photo = ImageTk.PhotoImage(self.image)
        self.canvas.grid(row=0,column=4)
        self.image = self.canvas.create_image(0, 0, anchor='nw',image=self.photo)

    def gen1(self):
        try:
                MinNum = int(self.txt2.get())
                MaxNum = int(self.txt3.get())
                Num = int(self.txt4.get())
        except ValueError:
                messagebox.showerror("Error", "Якщо не рвешся у висоту, шишок не наб'єш.")
        else:
                Nums = " "
                if MinNum <= MaxNum:
                        i = 0
                        while i < Num:
                                numOne = randint(MinNum, MaxNum)
                                Nums = Nums + ' ' + str(numOne)
                                i += 1
                        self.scrolltxt.config(state="normal") # enable the text box 
                        self.scrolltxt.delete(1.0, END)
                        self.scrolltxt.insert(INSERT, str(Nums) + "\n") 
                        self.scrolltxt.config(state="disabled") # disable the text box
                else:
                        messagebox.showerror("Error", "Якщо не рвешся у висоту, шишок не наб'єш.")
        

    def delete(self):
        self.txt4.delete(0, END)
        self.txt3.delete(0, END)
        self.txt2.delete(0, END)
        self.scrolltxt.config(state="normal") # enable the text box 
        self.scrolltxt.delete(1.0, END)
        self.scrolltxt.config(state="disabled") # disable the text box

    def clicked2(self):
        print("clicked2")
        self.image = Image.open("C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\11.png")  
        self.photo = ImageTk.PhotoImage(self.image)
        self.canvas.grid(row=0,column=4)
        self.image = self.canvas.create_image(0, 0, anchor='nw',image=self.photo)

    def clicked3(self):
        messagebox.showinfo("Це Звірополіс. Будь-хто може бути будь-ким.", "Хто сказав, що неможливе недосяжне?! Пошкодуйте цього дивака.")

    def inf(self):
        messagebox.showinfo("Info", "Лисичка замахалась")
    
app = App()

is it possible to implement this through tkinter?




Aucun commentaire:

Enregistrer un commentaire