gpt4 book ai didi

Python Tkinter : create a list with Entry, 并将更新的列表发送到另一个类

转载 作者:行者123 更新时间:2023-11-28 19:15:26 25 4
gpt4 key购买 nike

我的目标是使用 Tkinter 制作一个 GUI 程序,它制作一个带有一个按钮的窗口,该按钮打开另一个窗口,其中有两个标签,代表用户必须写入的某些点的坐标 x 和 y .该坐标必须插入列表中(例如:[[0,1],[2,3],...)。然后,我只想在 Canvas 上绘制这些点,使用函数 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

我有一些问题:

  1. 首先:这是用类创建和初始化窗口的优雅方式吗?因为我看到很多方法可以做到这一点
  2. 显然,当我在 AppUI 中打印 self.a 时,它会打印一个空列表,因为在上一节课中我初始化了 self.lista=[]!所以我需要更新变量 self.lista 的东西,以便它可以到达 AppUI 的所有点
  3. 为什么我点击 root_nodes 中的 END 按钮它不会被破坏?
  4. 最后一件事:类必须“交流”,因为我希望我的变量 lista 位于代码的许多“位置”:当然是在类 AppUI 中,我将 Canvas 放在其中(第一个 #here),但也在代码的主体(第二个#here),因为我必须用它做一些操作

非常感谢

最佳答案

My target is to make a GUI program, using Tkinter, that makes a window with one button

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)

which opens another window in which there are two lables, that represent the coordinates x and y of some points that the user has to write.

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()

Then, I just want to draw these points in the canvas, whit the function 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()

关于Python Tkinter : create a list with Entry, 并将更新的列表发送到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34098922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com