我的目标是使用Tkinter创建一个GUI程序,它创建一个带有一个按钮的窗口,打开另一个窗口,其中有两个标记,表示用户必须写入的某些点的坐标x和y 。必须将此坐标插入列表中(例如:[[0,1],[2,3],...)。然后,我只想在画布中绘制这些点,使用函数create_oval。 这是我的代码
from Tkinter import *
import tkMessageBox, tkFileDialog
class nodes(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, relief=SUNKEN, bd=2)
self.root_nodes = Tk()
self.root_nodes.geometry("200x150")
self.lista=[]
Label(self.root_nodes, text="x: ").pack()
self.x = Entry(self.root_nodes)
self.x.pack()
Label(self.root_nodes, text="y: ").pack()
self.y = Entry(self.root_nodes)
self.y.pack()
self.ok = Button(self.root_nodes, text="OK", command=self.OK)
Label(self.root_nodes, text="\n")
self.ok.pack()
self.fine = Button(self.root_nodes, text="END", command=self.end)
self.fine.pack()
def OK(self):
self.lista.append([float(self.x.get()),float(self.y.get())])
def end(self):
return self.lista
self.root_nodes.destroy()
class AppUI(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, relief=SUNKEN, bd=2)
def points():
x = nodes()
self.a = x.end()
print self.a
#here
self.frame = Frame(root)
self.point = Button(self.frame, text = "Points", command = points).pack()
self.frame.pack(side =LEFT, fill =BOTH)
self.canvas = Canvas(self, bg="white", width=400, height=400,bd=0, highlightthickness=0)
#for i in range (0, len(self.a)):
#self.canvas.create_oval(self.a[i][0] - 1 , self.a[i][1] - 1, self.a[i][0] + 1 , self.a[i][1] + 1)
self.canvas.pack()
try:
self.master.config(menu=self.barra_menu)
except AttributeError:
# master is a toplevel window (Python 1.4/Tkinter 1.63)
self.master.tk.call(master, "config", "-menu")
root = Tk()
root.geometry("400x400")
miaApp = AppUI(root)
miaApp.pack()
root.mainloop()
#here
我有一些问题:
非常感谢
答案 0 :(得分:0)
我的目标是使用Tkinter创建一个GUI程序,该程序使用一个按钮构建一个窗口
from Tkinter import *
class OneButton():
def __init__(self, master):
self.master=master
Button(master, text="Get Coordinates", command=self.get_coords).grid(row=0)
Button(master, text="Exit", command=master.quit).grid(row=1)
def get_coords(self):
self.N=Nodes(self.master)
打开另一个窗口,其中有两个标记,表示用户必须写入的某些点的坐标x和y。
class Nodes():
def __init__(self, master):
self.root_nodes = Toplevel(master)
self.root_nodes.geometry("200x150")
self.lista=[]
Label(self.root_nodes, text="x: ").grid(row=0, column=0)
self.x = Entry(self.root_nodes)
self.x.grid(row=0, column=1)
Label(self.root_nodes, text="y: ").grid(row=1, column=0)
self.y = Entry(self.root_nodes)
self.y.grid(row=1, column=1)
self.ok = Button(self.root_nodes, text="OK", command=self.OK)
## Label(self.root_nodes, text="\n")
self.ok.grid(row=5, column=0, columnspan=2)
self.fine = Button(self.root_nodes, text="Close this window", command=self.end)
self.fine.grid(row=10, columnspan=2)
def OK(self):
self.lista.append([float(self.x.get()),float(self.y.get())])
print self.lista
def end(self):
self.root_nodes.destroy()
然后,我只想在画布中绘制这些点,使用函数create_oval
class AppUI():
def __init__(self, master, lista):
self.master=master
self.frame = Toplevel(master)
self.canvas = Canvas(self.frame, bg="white", width=400, height=400,bd=0,
highlightthickness=0)
self.canvas.grid()
for x, y in lista:
print x, y
self.canvas.create_oval(x, y, x+10, y+10, fill="black")
root = Tk()
root.geometry("400x400")
miaApp = OneButton(root)
root.mainloop()