【问题标题】:How to make a button appear after a button click in tkinter?如何在 tkinter 中单击按钮后出现按钮?
【发布时间】:2022-01-03 15:37:34
【问题描述】:

我在这里遇到这个问题,我希望在用户单击主按钮 (b0) 后出现一个按钮(代码中的 b1)。

from tkinter import *
from tkinter import filedialog
import tkinter.messagebox
import os

def openword():
    my_program = filedialog.askopenfilename()
    os.system('"%s"' % my_program)

def btn_clicked():
    tkinter.messagebox.showinfo("Login","Login Success, Welcome!")

window = Tk()

window.geometry("1000x600")
window.configure(bg = "#293335")
canvas = Canvas(
    window,
    bg = "#293335",
    height = 600,
    width = 1000,
    bd = 0,
    highlightthickness = 0,
    relief = "ridge")
canvas.place(x = 0, y = 0)

background_img = PhotoImage(file = f"background.png")
background = canvas.create_image(
    508.5, 228.0,
    image=background_img)

entry0_img = PhotoImage(file = f"img_textBox0.png")
entry0_bg = canvas.create_image(
    166.0, 367.0,
    image = entry0_img)

entry0 = Entry(
    bd = 0,
    bg = "#ffffff",
    highlightthickness = 0)

entry0.place(
    x = 22, y = 351,
    width = 288,
    height = 30)

entry1_img = PhotoImage(file = f"img_textBox1.png")
entry1_bg = canvas.create_image(
    166.0, 456.0,
    image = entry1_img)

entry1 = Entry(
    bd = 0,
    bg = "#ffffff",
    highlightthickness = 0)

entry1.place(
    x = 22, y = 440,
    width = 288,
    height = 30)

img0 = PhotoImage(file = f"img0.png")
b0 = Button(
    image = img0,
    borderwidth = 0,
    highlightthickness = 0,
    command = btn_clicked,
    relief = "flat")

b0.place(
    x = 28, y = 500,
    width = 102,
    height = 38)

img1 = PhotoImage(file = f"img1.png")
b1 = Button(
    image = img1,
    borderwidth = 0,
    highlightthickness = 0,
    command = openword,
    relief = "flat")

b1.place(
    x = 766, y = 505,
    width = 213,
    height = 72)

window.resizable(False, False)
window.mainloop()

代码运行良好,但两个按钮同时出现。 我需要在用户按下b0 后出现b1

【问题讨论】:

  • 请提供minimal reproducible example。当里面有这么多不需要的图像时,运行你的代码真的很困难。
  • 然后将b1.place(...)移动到btn_clicked()内部。
  • 感谢@acw1668,我刚刚将 b1.place 移到函数内部并更改了一些东西,它工作正常

标签: python tkinter button


【解决方案1】:

就像 acw1668 建议的那样,将按钮放在单击另一个按钮时调用的函数中。这是一个例子:

import tkinter as tk
import tkinter.messagebox

def btn_clicked():
    tkinter.messagebox.showinfo("Login","Login Success, Welcome!")
    b2 = tk.Button(
        borderwidth = 0,
        highlightthickness = 0,
        command = btn_clicked,
        relief = "flat"
    )
    b2.place(
        x = 200, y = 200,
        width = 213,
        height = 72
    )

window = tk.Tk()

window.geometry("1000x600")
window.configure(bg = "#293335")

b1 = tk.Button(
    borderwidth = 0,
    highlightthickness = 0,
    command = btn_clicked,
    relief = "flat"
)

b1.place(
    x = 5, y = 5,
    width = 213,
    height = 72
)

window.resizable(False, False)
window.mainloop()

【讨论】:

  • 不要从模块中导入所有东西,只导入你需要的东西
  • 我的建议只是移动place(...) 行,而不是创建按钮。每当执行该函数时,您的代码都会创建新按钮。
  • 非常感谢你帮助了我
猜你喜欢
  • 2021-09-12
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 2021-12-19
  • 2012-01-15
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
相关资源
最近更新 更多