在GUI中显示功能

时间:2018-10-08 05:48:17

标签: python user-interface pycharm

我正在尝试为游戏制作一个“命令生成器”,用户可以在其中将数字输入文本框,而我的程序将为它们生成命令。我的工作几乎完美,但是生成的命令已打印到控制台,我希望它输入到GUI。

很抱歉,如果我的代码混乱或没有以最有效的方式完成,那么我是编程新手。

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()

为清楚起见,我希望将功能save()打印到GUI上

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先,您没有grid您的结果标签。 其次,您可以更改save()函数以每次都修改结果文本。

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()

这应该可以解决问题。