簡體   English   中英

使用tkinter模塊重疊狀態

[英]overlapping statuses using tkinter module

我正在嘗試使用tkinter模塊在GUI上工作。 我使用隨機問候語生成器創建了標簽。 但是,它們與先前生成的標簽重疊。這是代碼:

import tkinter
import random

window = tkinter.Tk()
# to rename the title of the window
window.title("GUI")

window.geometry("500x500")

#defining Functions
def search_greetings():
    phrases = ["Hallo ", "Hoi ", "Greetings "]
    name = str(entry1.get())
    text = ".Please enter your search term below."
    return phrases[random.randint(0, 2)] + name + text

def search_display():
    greeting = search_greetings()
    # This creates the text field
    greeting_display = tkinter.Label(window,text = search_greetings())
    greeting_display.grid(row=6,column=1)
    search_box = tkinter.Entry()
    search_box.grid(row=7)


# pack is used to show the object in the window
label = tkinter.Label(window, text = "Hello World! Welcome to my app")
label.grid(row = 0)

# creating 2 text labels and input labels

tkinter.Label(window, text = "Username").grid(row = 2) # this is placed in 1 0
# 'Entry' is used to display the input-field
entry1 = tkinter.Entry()
entry1.grid(row = 2, column = 1) # this is placed in 1 1

tkinter.Label(window, text = "Password").grid(row = 3) # this is placed in 2 0
tkinter.Entry().grid(row = 3, column = 1) # this is placed in 2 1

# 'Checkbutton' is used to create the check buttons
tkinter.Checkbutton(window, text = "Keep Me Logged In").grid(columnspan = 2) # 'columnspan' tells to take the width of 2 columns
                                                                             # you can also use 'rowspan' in the similar manner


# Submit button
button = tkinter.Button(text = "Submit",command = search_display).grid(row = 5)     

window.mainloop()

它會返回如下標簽:Greetings1234。請在下面輸入搜索詞。

G Hallo ashita。請在下面輸入搜索詞。

G Hallo ashita。請在下面輸入搜索詞。

請檢查代碼中的錯誤。

好像您每次都在制作一個新標簽。 您可以這樣編輯標簽的文本:

mylabel = tkinter.Label(root, text="First!")
mylabel["text"] = "Second!"

這將顯示“第二!” (打包后)。 打包標簽后,您甚至可以更改文本。

暫無
暫無

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

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