禁用提交按钮,直到所有字段都填入Python Tkinter

时间:2019-03-17 20:15:08

标签: python user-interface tkinter

我目前正在一个项目上,当我的程序启动时,需要在开始之前从两个播放器中删除姓名,这是我目前的程序方式,可以按Submit而不输入姓名,怎么办我预防吗?

非常感谢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()

所有帮助将是惊人的,再次感谢! :D

感谢大家的帮助。我设法找到了可行的解决方案!

1 个答案:

答案 0 :(得分:1)

您可以查看输入文本,例如

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