簡體   English   中英

在 GUI 中顯示函數

[英]Displaying a function in GUI

我正在嘗試為游戲制作一個“命令生成器”,用戶可以在其中將數字輸入到文本框中,我的程序將為它們生成命令。 我的這個工作幾乎完美,但生成的命令打印到控制台,我希望它輸入到 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 上

感謝您的幫助。

首先,您沒有對結果標簽進行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()

這應該可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM