Trying to implement two python files for alert messages

119 Views Asked by At

So I’m trying to implement one python file with if-conditions, and based on the condition I want to have an alert message prompt. For the 2nd python file, I wanted to keep the tkinter stuff separate so it’s cleaner for larger scale projects.

I got this to work but now I have 2 alert messages populating. I only want 1 alert message to pop up based on if condition.

#example1.py
import tkinter as tk
import tkinter.messagebox


alert = tk.Tk()
alert.withdraw()

def alertbox():
    alertbox.message_1=tk.messagebox.showwarning(title="Alert", message='Working')
    alertbox.message_2=tk.messagebox.showwarning(title="Alert", message='not working')

alertbox()
        
#    -----------
#example2.py
import example1

class Logic:
    def results():
        a = 100
        b = 10
        if a > b:
          example1.alertbox.message_1
        else:
          example1.alertbox.message_2
1

There are 1 best solutions below

0
Daniyal Warraich On

That is because the function you are calling shows two message boxes. You should create two separate functions:

example1.py:

import tkinter as tk
import tkinter.messagebox as msg

def message1():
    msg.showwarning(title="Alert", message="Message")

def message2():
    msg.showwarning(title="Alert", message="Message 2")

alert = Tk()
alert.withdraw()

You also didn't need the function call at the end of example1.py.

example2.py:

import example1

class Logic:
    def mymethod(self):
        if self.a > self.b:
            example1.message1()
        else:
            example1.message2()