Search code examples
pythontkinterpycharm

Displaying a function in GUI


I am trying to make a "command generator" for a game where the user can input numbers into text boxes and my program will generate the command for them. I have this working almost perfectly, but the generated command is printed to the console, I want it to input to the GUI.

Sorry if my code is messy or not done the most efficient way, I am brand new to programming.

from tkinter import *

#Create window that is 500x500
window = Tk()
window.geometry("500x500")

#This is the final command that is outputted
def save():
    print("/" + var2.get() + " " + entryx1.get() + " " + entryy1.get() + " " + entryz1.get() + " " + entryx2.get() + " " + entryy2.get()
          + " " + entryz2.get() + " " + var1.get())

#This is what you call when you want to display the dropdown input
var2 = StringVar()

#Creating the dropdown
drop2 = OptionMenu(window,var2,"fill","setblock")
drop2.configure(font=("Arial",10))

#This makes the text that displays in the window
coordx1 = Label(window,text="First X Coordinate: ")
coordy1 = Label(window,text="First Y Coordinate: ")
coordz1 = Label(window,text="First Z Coordinate: ")
coordx2 = Label(window,text="Second X Coordinate: ")
coordy2 = Label(window,text="Second Y Coordinate: ")
coordz2 = Label(window,text="Second Z Coordinate: ")
result = Label(window,text=save)

#This makes the text boxes that the user types in
entryx1 = Entry(window)
entryy1 = Entry(window)
entryz1 = Entry(window)
entryx2 = Entry(window)
entryy2 = Entry(window)
entryz2 = Entry(window)

#This is the submit button
submit = Button(window,text="Submit",command=save)

#Second dropdown variable
var1=StringVar()

#Creation of second dropdown list
drop1 = OptionMenu(window,var1,"destroy","hollow","keep","outline","replace")
drop1.configure(font=("Arial",10))

dropdownLabel = Label(window,text="Selector: ")
dropdownLabel2 = Label(window,text="Command: ")

#This says what goes where. 'E' and 'W' represent East and West
coordx1.grid(row=1,sticky=E)
coordy1.grid(row=2,sticky=E)
coordz1.grid(row=3,sticky=E)
coordx2.grid(row=4,sticky=E)
coordy2.grid(row=5,sticky=E)
coordz2.grid(row=6,sticky=E)
entryx1.grid(row=1,column=1)
entryy1.grid(row=2,column=1)
entryz1.grid(row=3,column=1)
entryx2.grid(row=4,column=1)
entryy2.grid(row=5,column=1)
entryz2.grid(row=6,column=1)
dropdownLabel.grid(row=7,column=0,sticky=E)
dropdownLabel2.grid(row=0,column=0,sticky=E)
drop1.grid(row=7,column=1,sticky=W)
drop2.grid(row=0,column=1,sticky=W)
submit.grid(row=8,columnspan=2,sticky=E)

mainloop()

To be clear, I want the function save() to be printed onto the GUI


Solution

  • First, you didn't grid your result Label. Secondly, you can change your save() function to modify the result text everytime.

    from tkinter import *
    
    #Create window that is 500x500
    window = Tk()
    window.geometry("500x500")
    
    #This is the final command that is outputted
    def save():
        result.config(text="/" + var2.get() + " " + entryx1.get() + " " + entryy1.get() + " " + entryz1.get() + " " + entryx2.get() + " " + entryy2.get()+ " " + entryz2.get() + " " + var1.get())
    
    #This is what you call when you want to display the dropdown input
    var2 = StringVar()
    
    #Creating the dropdown
    drop2 = OptionMenu(window,var2,"fill","setblock")
    drop2.configure(font=("Arial",10))
    
    #This makes the text that displays in the window
    coordx1 = Label(window,text="First X Coordinate: ")
    coordy1 = Label(window,text="First Y Coordinate: ")
    coordz1 = Label(window,text="First Z Coordinate: ")
    coordx2 = Label(window,text="Second X Coordinate: ")
    coordy2 = Label(window,text="Second Y Coordinate: ")
    coordz2 = Label(window,text="Second Z Coordinate: ")
    result = Label(window,text=save)
    
    #This makes the text boxes that the user types in
    entryx1 = Entry(window)
    entryy1 = Entry(window)
    entryz1 = Entry(window)
    entryx2 = Entry(window)
    entryy2 = Entry(window)
    entryz2 = Entry(window)
    
    #This is the submit button
    submit = Button(window,text="Submit",command=save)
    
    #Second dropdown variable
    var1=StringVar()
    
    #Creation of second dropdown list
    drop1 = OptionMenu(window,var1,"destroy","hollow","keep","outline","replace")
    drop1.configure(font=("Arial",10))
    
    dropdownLabel = Label(window,text="Selector: ")
    dropdownLabel2 = Label(window,text="Command: ")
    
    #This says what goes where. 'E' and 'W' represent East and West
    coordx1.grid(row=1,sticky=E)
    coordy1.grid(row=2,sticky=E)
    coordz1.grid(row=3,sticky=E)
    coordx2.grid(row=4,sticky=E)
    coordy2.grid(row=5,sticky=E)
    coordz2.grid(row=6,sticky=E)
    entryx1.grid(row=1,column=1)
    entryy1.grid(row=2,column=1)
    entryz1.grid(row=3,column=1)
    entryx2.grid(row=4,column=1)
    entryy2.grid(row=5,column=1)
    entryz2.grid(row=6,column=1)
    dropdownLabel.grid(row=7,column=0,sticky=E)
    dropdownLabel2.grid(row=0,column=0,sticky=E)
    drop1.grid(row=7,column=1,sticky=W)
    drop2.grid(row=0,column=1,sticky=W)
    submit.grid(row=8,columnspan=2,sticky=E)
    
    result.grid(row=9,columnspan=1)
    
    window.mainloop()
    

    This should do the trick.