【发布时间】: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 移到函数内部并更改了一些东西,它工作正常