(特金特)我正在尝试创建一个按钮,单击一个新窗口会弹出一个带有标签文本,但错误显示它是无效的语法



我正在创建一个手机拨号器,我的问题是,当我按下"call"按钮,将出现一个新窗口,显示文本"正在拨号…"。每当我运行它时,总是会出现语法错误。问题出在def calling_win():语句中。有人能修好吗?

import tkinter as tk
def btn_press(key):
numberentry.insert(tk.END, key)
def calling_win():
window = tk.Tk()
window.geometry('250x200')
dial_label = (window, text='Dialing a number...', bg='black', fg='white', font='times new roman', 12, 'bold') .place(padx=30, pady=70)
dialer = tk.Tk()
dialer.title("Mobile Phone Dialer")
dialer.resizable(0,0)
btn_options = dict(padx=10, pady=10, bd= 5, bg='black', fg='white', font=('times new roman', 12, 'bold'))
tk.Button(dialer, btn_options, text=1, command=lambda: btn_press(1)).grid(row=1, column=0)
tk.Button(dialer, btn_options, text=2, command=lambda: btn_press(2)) .grid(row=1, column=1)
tk.Button(dialer, btn_options, text=3, command=lambda: btn_press(3)) .grid(row=1, column=2)
tk.Button(dialer, btn_options, text=4, command=lambda: btn_press(4)) .grid(row=2, column=0)
tk.Button(dialer, btn_options, text=5, command=lambda: btn_press(5)) .grid(row=2, column=1)
tk.Button(dialer, btn_options, text=6, command=lambda: btn_press(6)) .grid(row=2, column=2)
tk.Button(dialer, btn_options, text=7, command=lambda: btn_press(7)) .grid(row=3, column=0)
tk.Button(dialer, btn_options, text=8, command=lambda: btn_press(8)) .grid(row=3, column=1)
tk.Button(dialer, btn_options, text=9, command=lambda: btn_press(9)) .grid(row=3, column=2)
tk.Button(dialer, btn_options, text='*', command=lambda: btn_press("*")) .grid(row=4, column=0)
tk.Button(dialer, btn_options, text=0, command=lambda: btn_press(0)) .grid(row=4, column=1)
tk.Button(dialer, btn_options, text='#', command=lambda: btn_press("#")) .grid(row=4, column=2)
tk.Button(dialer, btn_options, text='Call', command=calling_win) .grid(row=5, column=1)
numberentry = tk.Entry(dialer,bd = 10, width=20,bg='black', fg='white')
numberentry.grid(row=0, column=1, sticky=tk.N)
dialer.mainloop()

首先你忘记了tk.Label,所以你得到了错误。其次,place没有padxpady,所以有第二个错误。此外,你必须把字体放在括号里,否则tkinter会把字体参数作为其他标签参数。

工作代码:

import tkinter as tk
def btn_press(key):
numberentry.insert(tk.END, key)
def calling_win():
window = tk.Tk()
window.geometry('250x200')
dial_label = tk.Label(window, text='Dialing a number...', bg='black', fg='white', font=('times new roman', 12, 'bold')).place(x=30, y=70)
dialer = tk.Tk()
dialer.title("Mobile Phone Dialer")
dialer.resizable(0,0)
btn_options = dict(padx=10, pady=10, bd= 5, bg='black', fg='white', font=('times new roman', 12, 'bold'))
tk.Button(dialer, btn_options, text=1, command=lambda: btn_press(1)).grid(row=1, column=0)
tk.Button(dialer, btn_options, text=2, command=lambda: btn_press(2)) .grid(row=1, column=1)
tk.Button(dialer, btn_options, text=3, command=lambda: btn_press(3)) .grid(row=1, column=2)
tk.Button(dialer, btn_options, text=4, command=lambda: btn_press(4)) .grid(row=2, column=0)
tk.Button(dialer, btn_options, text=5, command=lambda: btn_press(5)) .grid(row=2, column=1)
tk.Button(dialer, btn_options, text=6, command=lambda: btn_press(6)) .grid(row=2, column=2)
tk.Button(dialer, btn_options, text=7, command=lambda: btn_press(7)) .grid(row=3, column=0)
tk.Button(dialer, btn_options, text=8, command=lambda: btn_press(8)) .grid(row=3, column=1)
tk.Button(dialer, btn_options, text=9, command=lambda: btn_press(9)) .grid(row=3, column=2)
tk.Button(dialer, btn_options, text='*', command=lambda: btn_press("*")) .grid(row=4, column=0)
tk.Button(dialer, btn_options, text=0, command=lambda: btn_press(0)) .grid(row=4, column=1)
tk.Button(dialer, btn_options, text='#', command=lambda: btn_press("#")) .grid(row=4, column=2)
tk.Button(dialer, btn_options, text='Call', command=calling_win) .grid(row=5, column=1)
numberentry = tk.Entry(dialer,bd = 10, width=20,bg='black', fg='white')
numberentry.grid(row=0, column=1, sticky=tk.N)
dialer.mainloop()

相关内容

  • 没有找到相关文章

最新更新