![](/img/trans.png)
[英]Adding image to Toplevel window using Tkinter is getting displayed in Mainwindow instead
[英]Tkinter: Getting information from mainwindow to a new window
我正在使用 Tkinter,我將從用戶那里獲取數據並將它們顯示在新的 window 中。但是我正在努力在新的 window 中顯示信息。我嘗試使用 the.get() 並只是打印但是新的 window 打開時沒有數據。
我試過使用 messageBox 但它似乎也不是我想要發生的事情。
這是代碼
from tkinter import *
class peronalInfo_GUI():
def __init__(self):
#create window and set title
self.mainWindow = Tk()
self.mainWindow.title("Property Taxes)")
window_width = 330
window_height = 150
# get the screen dimension
screen_width = self.mainWindow.winfo_screenwidth()
screen_height = self.mainWindow.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
# set the position of the window to the center of the screen
self.mainWindow.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
#self.mainWindow.geometry("250x160")
#Label 1 --> User Input name
self.nameLabel = Label(self.mainWindow, text = "Enter your name: ")
self.nameTextBox = Entry(self.mainWindow, width = 25)
self.nameLabel.grid(row = 0, column = 0)
self.nameTextBox.grid(row = 0, column = 1)
#Label 2 --> User Input address
self.addressLabel = Label(self.mainWindow, text = "Enter address: ")
self.addressTextBox = Entry(self.mainWindow, width = 25)
self.addressLabel.grid(row = 1, column = 0)
self.addressTextBox.grid(row = 1, column = 1)
#Label 3 --> User Input phone number
self.phoneNumberLabel = Label(self.mainWindow, text = "Enter phone number: ")
self.phoneNumberTextBox = Entry(self.mainWindow, width = 25)
self.phoneNumberLabel.grid(row = 2, column = 0)
self.phoneNumberTextBox.grid(row = 2, column = 1)
#Label 4 --> User input college major
self.collegeMajorLabel = Label(self.mainWindow, text = "Enter your college major: ")
self.collegeMajorTextBox = Entry(self.mainWindow, width = 25)
self.collegeMajorLabel.grid(row = 3, column = 0)
self.collegeMajorTextBox.grid(row = 3, column=1)
#Create Buttons for event handling
self.displayInfo = Button(self.mainWindow, text = "Display Information", bg = "#BB8FCE", command = self.openNewWindow)
self.exitButton = Button(self.mainWindow, text = "Exit", bg = "#F1948A", command = self.mainWindow.destroy)
self.displayInfo.grid( row = 5, column = 0, padx = 15, pady = 15)
self.exitButton.grid ( row = 5, column= 1)
#Loop
self.mainWindow.mainloop()
def openNewWindow(self):
newWindow = Toplevel(self.mainWindow)
newWindow.title("Display Info")
newWindow.geometry("350x150")
#Display User Info
self.displayName = Label(text = self.nameTextBox.get(), row = 1)
self.displayAddress = Label(text = self.addressTextBox.get(), row = 2)
self.displayNumber = Label(text = self.phoneNumberTextBox.get(), row = 3)
self.displayMajor = Label(text = self.collegeMajorTextBox.get(), row = 4)
def main():
callGUI = peronalInfo_GUI()
main()
您似乎沒有在控制台中運行代碼,因為您會看到錯誤
_tkinter.TclError: unknown option "-row"
這可以部分解決問題
我看到兩個問題:
第一的:
你用
var = Label(..., row=1)
但它必須是
var = Label(...)
var.grid(row=1)
這會產生錯誤。
第二:
您創建Label
而沒有parent
項作為第一個值 - 因此它將使用mainWindow
作為parent
項並且它將在第一個 window 而不是新的 window 中顯示Label
。
你需要:
Label(newWindow, ...)
def openNewWindow(self):
newWindow = Toplevel(self.mainWindow)
newWindow.title("Display Info")
newWindow.geometry("350x150")
#Display User Info
self.displayName = Label(newWindow, text=self.nameTextBox.get())
self.displayName.grid(row=1)
self.displayAddress = Label(newWindow, text=self.addressTextBox.get())
self.displayAddress.grid(row=2)
self.displayNumber = Label(newWindow, text=self.phoneNumberTextBox.get())
self.displayNumber.grid(row=3)
self.displayMajor = Label(newWindow, text=self.collegeMajorTextBox.get())
self.displayMajor.grid(row=4)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.