改变这个代码: from tkinter import * # Import tkinter def procK() : print("OK button is clicked") def processCancel(): print ("Cancel button is clicked") root = Tk # Create a root window btOK = Button (root, text = "OK", fg = "red", command = prock) btCancel = Button (root, text = "Cancel", bg = "yellow' command = processCancel) btOK. pack () # Place the button in the window btCancel.pack() # Place the button in the window root.mainloop() # Create an event loop
时间: 2024-01-25 14:04:43 浏览: 111
这段代码存在一些语法上的错误,需要进行修正。下面是修改后的代码:
```python
from tkinter import *
def procK():
print("OK button is clicked")
def processCancel():
print("Cancel button is clicked")
root = Tk() # Create a root window
btOK = Button(root, text="OK", fg="red", command=procK)
btCancel = Button(root, text="Cancel", bg="yellow", command=processCancel)
btOK.pack() # Place the button in the window
btCancel.pack() # Place the button in the window
root.mainloop() # Create an event loop
```
主要修改包括:
1. 创建 root 窗口时应该加上括号,即 `root = Tk()`。
2. 创建 btCancel 按钮时,`bg` 参数的值应该是 `"yellow"`,而不是 `"yellow'"`,并且 `command` 参数的赋值语句末尾缺少了一个逗号。
3. 在创建按钮后,应该分别调用 `pack()` 方法将它们放置到窗口中。
阅读全文