Jayode18
Jayode18

Reputation: 3

Disable submit button until all fields are filled in Python Tkinter

I'm currently working on a project and when my program launches it needs to get the names off of two players before beginning, the way my program is at the moment, it is possible to press submit without entering names, how do I prevent this?

Many Thanks Jayode18

# Program by Jack O'Donnell (Jayode18 StackOverflow/GitHub)
# Date Started: 16th March 2019


# Import winsound and create functions for each of the sound effects & their functionalities.
import tkinter
import time
from tkinter import *
import random
import winsound

def gameOver():
    winsound.PlaySound("Gameover", winsound.SND_FILENAME)


def pointsDrop():
    winsound.PlaySound("Points drop", winsound.SND_FILENAME)


def pointsGain():
    winsound.PlaySound("Points gain", winsound.SND_FILENAME)


def flipCoin():
    winsound.PlaySound("coinflip", winsound.SND_FILENAME)


def rollDice():
    winsound.PlaySound("Dice", winsound.SND_FILENAME)


# Other Definitions #
def goToPrimary():
    primaryWindow = tkinter.Tk()
    primaryWindow.iconbitmap("icon.ico")
    primaryWindow.geometry("500x500")
    primaryWindow.title("YGO Calculator ver. 1.0 ALPHA")

    gainLPButton = Button(primaryWindow, text = "LP Gain", command = pointsGain)
    gainLPButton.grid(row = 2, column = 1)

    loseLPButton = Button(primaryWindow, text = "LP Loss", command = pointsDrop)
    loseLPButton.grid(row = 2, column = 2)


    gameOverButton = Button(primaryWindow, text = "LP = 0", command = gameOver)
    gameOverButton.grid(row = 2, column = 3)


    flipCoinButton = Button(primaryWindow, text = "Coin Toss", command = flipCoin)
    flipCoinButton.grid(row = 2, column = 4)

    rollDiceButton = Button(primaryWindow, text = "Roll Dice", command = rollDice)
    rollDiceButton.grid(row = 2, column = 5)


# Button Commands #

def OnSubmit():
  e = entry_duelist.get()
  print(e)
  time.sleep(0.25)
  window.destroy()
  goToPrimary()

# import tkinter and create the window window, then populate it with buttons to text window.    
window = tkinter.Tk()
window.resizable(width = False, height = False)
window.title("YGO Calculator ver. 1.0 ALPHA")
window.iconbitmap('icon.ico') # Give window the correct icon


mainWindow = Frame(window)

window.geometry("180x75")


label_duelist = Label(mainWindow, text="Duelist 1:")
label_duelist_2 = Label(mainWindow, text="Duelist 2:")

entry_duelist = Entry(mainWindow)
entry_duelist_2 = Entry(mainWindow)

label_duelist.grid(row = 4, column = 0)
label_duelist_2.grid(row = 5, column = 0)

entry_duelist.grid(row = 4, column = 1, columnspan = 4)
entry_duelist_2.grid(row = 5, column = 1, columnspan = 4)

submit_button = Button(mainWindow, text = "Submit", command = OnSubmit)

submit_button.grid(row = 7, column = 4, columnspan = 2)

mainWindow.grid(row = 5, column = 0)

window.mainloop()

All help would be amazing, thanks again! :D

Thank you everyone for your help. I managed to find a working solution!

Upvotes: 0

Views: 683

Answers (1)

Pierre
Pierre

Reputation: 163

You can check the entry texts for example

if entry1.get() != "" And entry2.get() != "":
    # entries are not empty, do something 

Upvotes: 1

Related Questions