Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PRNG, python. 1 button – 2 function?

help me please. It is necessary that when the ‘Generate’ button(gen) is pressed, 2 functions (clicked1, gen1) are executed. But it only run clicked1.

I read that I can use lambda; or create a separate function that includes the ones you need. But gen1 still fails.
but the most important thing is that
when I removed clicked1 from the code completely, then gen1 didn’t work either(

import tkinter
from tkinter import *  
from tkinter import messagebox, scrolledtext
from tkinter.ttk import Progressbar  
from tkinter import ttk   
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, text="Generate", command = lambda:[self.clicked1(), self.gen1()])
        self.gen.grid(row=4, column=6)

        self.canvas = tkinter.Canvas(self.window, height=250, 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, text="Delete", command=self.delete)  
        self.btn.grid(column=6, row=5)

        self.exit = Button(self.window, text="Exit", command=quit)
        self.exit.grid(column=6, row=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", "Please enter correct numbers!")
        else:
                Nums = " "
                if MinNum <= MaxNum:
                        i = 0
                        while i < Num:
                                numOne = randint(MinNum, MaxNum)
                                Nums = Nums + ' ' + str(numOne)
                                i += 1
                        self.scrolltxt.delete(1.0, END) 
                        self.scrolltxt.insert(INSERT, str(Nums) + "\n") 
                else:
                        messagebox.showerror("Error", "Please enter correct numbers!")
        
    def delete(self):
        self.scrolltxt.delete(1.0, END)
        print("clicked1")
        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)
    
app= App()

I don’t understand what I’m doing wrong
I hope I explained the problem clearly

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Both the clicked1() and gen1() are executed when the Generate button is clicked. However nothing will be inserted into the text box because the text box is disabled.

You need to enable the text box before inserting text and disabled it back after:

self.scrolltxt.config(state="normal") # enable the text box
self.scrolltxt.delete("1.0", END) # better use "1.0" instead of 1.0 
self.scrolltxt.insert(INSERT, str(Nums) + "\n")
self.scrolltxt.config(state="disabled") # disable the text box
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading