gpt4 book ai didi

python - 在 GUI 中显示函数

转载 作者:行者123 更新时间:2023-12-01 08:57:28 25 4
gpt4 key购买 nike

我正在尝试为游戏制作一个“命令生成器”,用户可以在文本框中输入数字,我的程序将为它们生成命令。我的这个工作几乎完美,但是生成的命令被打印到控制台,我希望它输入到 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 上

最佳答案

首先,您没有网格您的结果标签。其次,您可以更改 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()

这应该可以解决问题。

关于python - 在 GUI 中显示函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52696075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com