需要更改图片的PRNG(伪随机数生成器)Python代码部分。

huangapple go评论58阅读模式
英文:

prng,python, tkinter,need to change picture

问题

以下是代码的翻译部分:

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", "If you don't jump, you won't hit your head.")
        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", "If you don't jump, you won't hit your head.")

    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("This is Zootopia. Anyone can be anything.", "Who said the impossible is unattainable?! Regret this oddity.")

    def inf(self):
        messagebox.showinfo("Info", "The fox waved.")
    
app = App()

是的,这段代码是使用Tkinter实现的,用于创建一个简单的GUI应用程序。

英文:

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(&quot;Генератор&quot;) 
self.window[&#39;bg&#39;] = &#39;#FFF5EE&#39;
self.window.geometry(&#39;660x550&#39;) 
self.window.resizable(False,False)
self.lb1 = Label(self.window, text=&quot;Enter:&quot;, background=&#39;#FFF5EE&#39;, font = (&quot;Comic Sans MS&quot;, 14))  
self.lb1.grid(column=0, row=2) 
self.lb2 = Label(self.window, text=&quot;min(1-999)&quot;,background=&#39;#FFF5EE&#39;, font = (&quot;Comic Sans MS&quot;, 12))  
self.lb2.grid(column=1, row=3) 
self.lb3 = Label(self.window, text=&quot;max(1-999)&quot;, background=&#39;#FFF5EE&#39;, font = (&quot;Comic Sans MS&quot;, 12))  
self.lb3.grid(column=1, row=4) 
self.lb4 = Label(self.window, text=&quot;amount of numbers&quot;, background=&#39;#FFF5EE&#39;, font = (&quot;Comic Sans MS&quot;, 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=&#39;disabled&#39;)
self.scrolltxt.grid(row=1, column=2, columnspan=3, padx=10, pady=10)
self.image = Image.open(&quot;C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\11.png&quot;)
self.photo = ImageTk.PhotoImage(self.image)
self.gen = Button(self.window, width = 15, text=&quot;Generate&quot;, 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=&#39;nw&#39;, image = self.photo)
self.btn = Button(self.window, width = 15, text=&quot;Delete&quot;, command=lambda:[self.delete(),self.clicked2()])  
self.btn.grid(column=6, row=5)
self.exit = Button(self.window, width = 15, text=&quot;Exit&quot;, command=lambda: [self.clicked3(), quit()])
self.exit.grid(column=6, row=6)
self.i = Button(self.window, width = 8,text = &quot;i&quot;, font = (&quot;Eras Bold ITC&quot;, 10) , command = self.inf)
self.i.grid(row = 0,column = 6)
self.window.mainloop()
def clicked1(self):
print(&quot;clicked1&quot;)
self.image = Image.open(&quot;C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\22.png&quot;)  
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.grid(row=0,column=4)
self.image = self.canvas.create_image(0, 0, anchor=&#39;nw&#39;,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(&quot;Error&quot;, &quot;Якщо не рвешся у висоту, шишок не наб&#39;єш.&quot;)
else:
Nums = &quot; &quot;
if MinNum &lt;= MaxNum:
i = 0
while i &lt; Num:
numOne = randint(MinNum, MaxNum)
Nums = Nums + &#39; &#39; + str(numOne)
i += 1
self.scrolltxt.config(state=&quot;normal&quot;) # enable the text box 
self.scrolltxt.delete(1.0, END)
self.scrolltxt.insert(INSERT, str(Nums) + &quot;\n&quot;) 
self.scrolltxt.config(state=&quot;disabled&quot;) # disable the text box
else:
messagebox.showerror(&quot;Error&quot;, &quot;Якщо не рвешся у висоту, шишок не наб&#39;єш.&quot;)
def delete(self):
self.txt4.delete(0, END)
self.txt3.delete(0, END)
self.txt2.delete(0, END)
self.scrolltxt.config(state=&quot;normal&quot;) # enable the text box 
self.scrolltxt.delete(1.0, END)
self.scrolltxt.config(state=&quot;disabled&quot;) # disable the text box
def clicked2(self):
print(&quot;clicked2&quot;)
self.image = Image.open(&quot;C:\\Users\\ПК\\OneDrive\\Рабочий стол\\лб1\\11.png&quot;)  
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.grid(row=0,column=4)
self.image = self.canvas.create_image(0, 0, anchor=&#39;nw&#39;,image=self.photo)
def clicked3(self):
messagebox.showinfo(&quot;Це Звірополіс. Будь-хто може бути будь-ким.&quot;, &quot;Хто сказав, що неможливе недосяжне?! Пошкодуйте цього дивака.&quot;)
def inf(self):
messagebox.showinfo(&quot;Info&quot;, &quot;Лисичка замахалась&quot;)
app = App()

is it possible to implement this through tkinter?

答案1

得分: 0

self.gencommand参数更改为:

self.gen = Button(self.window, width=15, text="Generate", command=self.gen1)

clicked1()的调用移到gen1()if块中,以便只在满足条件时调用:

def gen1(self):
    try:
        MinNum = int(self.txt2.get())
        MaxNum = int(self.txt3.get())
        Num = int(self.txt4.get())
    except ValueError:
        messagebox.showerror(
            "Error",
            "If you don't aim high, you won't hit the target."
        )
    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")  # 启用文本框
            self.scrolltxt.delete(1.0, END)
            self.scrolltxt.insert(INSERT, str(Nums) + "\n")
            self.scrolltxt.config(state="disabled")  # 禁用文本框
            self.clicked1()  # 调用 'clicked1' 方法
        else:
            messagebox.showerror(
                "Error",
                "If you don't aim high, you won't hit the target."
            )
英文:

Change the command argument of self.gen to:

self.gen = Button(self.window, width = 15, text=&quot;Generate&quot;, command=self.gen1)

And move the call to clicked1() into the if block of gen1() so it will only be called when conditions are accepted

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

huangapple
  • 本文由 发表于 2023年2月16日 02:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75464102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定