1

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
  • I didn't understand what you are trying to do? You want if else in second file or first? What you exactly want to do is second file? – imxitiz Jul 28 '21 at 03:50
  • Basically how I want to incorporate this is within the second example code given. I want the if-else condition in example2.py and based on condition, I want to call on tkinter alert messages. In example1.py I want to declare all tkinter stuff in there, but not sure how to assign variables that can be imported into example 2 for each message – Brett Martin Jul 28 '21 at 04:06
  • I think you want to remove the function call, `alertbox()`, at the end of file 1. – Delrius Euphoria Jul 28 '21 at 05:05

1 Answers1

0

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()
Daniyal Warraich
  • 446
  • 3
  • 13