Python TkInter delete childs's grid - python-2.7

How can I clear the grid into a canvas in Tkinter?
Actually i have this :
photoCanvas = Canvas(photoFrame,bg='#E5E7E9')
rowPhoto = 0
columnPhoto = 0
for i in range(0, len(listPhotos), 1):
panel = Button(photoCanvas, image = listPhotos[i], borderwidth=0, height = 200, width = 200)
panel.grid(row=rowPhoto, column=columnPhoto, padx=5, pady=5, sticky="nsew")
if columnPhoto < 3:
columnPhoto += 1
else:
rowPhoto += 1
columnPhoto = 0
And i want to delete all my buttons.
Thx

If you want to delete all widgets inside another widget -- such as all buttons in your canvas -- you can simply iterate over all of the children of the widget and call the destroy method.
For example:
for widget in photoCanvas.winfo_children():
widget.destroy()

Related

Add new column to a QTableWidget

I need to add a new column to an existing QTableWidget whenever a button is pressed, something like:
ui->tableWidget->addColumn();
The best way I could find was:
ui->tableWidget->setColumnCount(ui->tableWidget->horizontalHeader()->count() + 1);
column_index = self.table_name.columnCount()
self.table_name.setRowCount(1)
self.table_name.setColumnCount(column_index + 1)
self.tb_widget = QWidget()
picture = "C:\\Users\\usa\\Desktop\\test.png"
pixmap = QPixmap(picture).scaled(QSize(160, 90))
tb_item_img = QLabel()
tb_item_img.setPixmap(pixmap)
# tb_item_img.setFixedSize(160, 90)
tb_item_time = QLabel()
tb_item_time.setText("00:00:00")
self.tb_item_btb.setProperty('time', tb_item_time.text())
self.tb_item_btb.clicked.connect(self.clickTableCut)
self.table_name.setColumnWidth(column_index, 160)
self.table_name.setRowHeight(0, 90)
self.table_name.horizontalHeader().setSectionResizeMode(column_index, QtWidgets.QHeaderView.Fixed)
self.table_name.setCellWidget(0, column_index, self.tb_widget)
# click
def clickTableCut(self, ):
button = self.sender()
if button:
t = button.property('time')
print(t)
column_index = self.table_name.indexAt(button.pos()).column()
# if remove an item
self.table_name.removeColumn(column_index)

getting the value to the Entry using tkinter

I want to know how to display the values using Entry? i mean for instance if i have a function like:
def open():
path=tkFileDialog.askopenfilename(filetypes=[("Image File",'.jpg')])
blue, green, red = cv2.split(path)
total = path.size
B = sum(blue) / total
G = sum(green) / total
R = sum(red) / total
B_mean1.append(B)
G_mean1.append(G)
R_mean1.append(R)
blue.set(B_mean1)
green.set(G_mean1)
red.set(R_mean1)
root = Tk()
blue_label = Label(app,text = 'Blue Mean')
blue_label.place(x = 850,y = 140)
blue = IntVar(None)
blue_text = Entry(app,textvariable = blue)
blue_text.place(x = 1000,y = 140)
green_label = Label(app,text = 'Green Mean')
green_label.place(x = 850,y = 170)
green = IntVar(None)
green_text = Entry(app,textvariable = green)
green_text.place(x = 1000,y = 170)
red_label = Label(app,text = 'Red Mean')
red_label.place(x = 850,y = 200)
red = IntVar(None)
red_text = Entry(app,textvariable = red)
red_text.place(x = 1000,y = 200)
button = Button(app, text='Select an Image',command = open)
button.pack(padx = 1, pady = 1,anchor='ne')
button.place( x = 650, y = 60)
root.mainloop()
I have specified all the necessary imports and list variables. Still the value is not being displayed in the Entry field.
like if i get the value as :
blue mean = 37,
green mean = 36,
red mean = 41
and it will print in the console but not in the window. How can I achieve this?
Any suggestions are welcome!
Thanks for your supports!
Firstly I want to recommend that instead of using the place method you use the grid method. It is a lot faster and allows you to do things in a much more orderly fashion - especially in this application. Note that there are other geometry managers - like pack - which are good in yet other instances. Anyways, your GUI:
root = Tk()
blue_label = Label(root, text="Blue mean:")
blue_label.grid(row=1, column=1, sticky="w")
blue_entry = Entry(root)
blue_entry.grid(row=1, column=2)
green_label = Label(root, text="Green mean:")
green_label.grid(row=2, column=1, sticky="w")
green_entry = Entry(root)
green_entry.grid(row=2, column=2)
red_label = Label(root, text="Red mean:")
red_label.grid(row=3, column=1, sticky="w")
red_entry = Entry(root)
red_entry.grid(row=3, column=2)
root.mainloop()
That makes your GUI display nicely and without the manual geometry. Note that sticky just sets how the text is justified ("w" means west, and so the text justifies left).
Now, for actually displaying the text, all you have to do is:
blue_entry.insert(0, "blue-mean-value")
green_entry.insert(0, "green-mean-value")
red_entry.insert(0, "red-mean-value")
And if you are overwriting any text that is already there just do:
blue_entry.delete(0, END) # And green_entry and red_entry

Aligning Label in Frame Tkinter

I am new to Python and even newer to Tkinter.
I am currently practicing how to use Frames and Labels and
the problem I am encountering is, when I put Labels on a frame with some buttons next to each label,
the alignment is not good to look at.
Here is the code:
from Tkinter import *
class GUI():
def __init__(self):
self.namelist = ["Mark","Anna","Jason","Lenna","Leo","Zucharich","Robinson","AReallyLongNameThatMightExist"]
self.canvas = Canvas(width=1200,height=700)
self.canvas.pack(expand=YES,fill=BOTH)
def Friends(self):
controlframe = Frame(self.canvas)
controlframe.place(x=600,y=300)
#Frame for showing names of friends
for x in self.namelist:
frame = Frame(controlframe)
frame.pack()
Name = Label(frame,text="%s "%x).pack(side=LEFT)
chatButton = Button(frame,text="Chat").pack(side=LEFT)
delButton = Button(frame,text="Delete").pack(side=LEFT)
setcloseButton = Button(frame,text="Set Close").pack(side=LEFT)
setgroupButton = Button(frame,text="Set Group").pack(side=LEFT)
mainloop()
GUI = GUI()
GUI.Friends()
What should I do so that the alignment of the Label(=name) and the button is equal to the other ones so that they will form a shape of a rectangle and not some zigzag?
It is almost always better in Tk to use the grid geometry manager. It is much more flexible once you come to understand how it works. Converting your example to use grid solves your problem as shown below but you should experiment with it a bit. Try removing the 'sticky="W"' from the label for instance and see that the centering of the widgets within the row or column can be controlled. To get your frame responding to resizes sensibly you should investigate the columnconfigure and rowconfigure options for the grid geometry management as well.
from Tkinter import *
class GUI():
def __init__(self):
self.namelist = ["Mark","Anna","Jason","Lenna",
"Leo","Zucharich","Robinson",
"AReallyLongNameThatMightExist"]
self.canvas = Canvas(width=1200,height=700)
self.canvas.pack(expand=YES,fill=BOTH)
def Friends(self):
frame = Frame(self.canvas)
frame.place(x=600,y=300)
#Frame for showing names of friends
row = 0
for x in self.namelist:
label = Label(frame,text="%s "%x)
chatButton = Button(frame,text="Chat")
delButton = Button(frame,text="Delete")
setcloseButton = Button(frame,text="Set Close")
setgroupButton = Button(frame,text="Set Group")
label.grid(row=row, column=0, sticky="W")
chatButton.grid(row=row, column=1)
delButton.grid(row=row, column=2)
setcloseButton.grid(row=row, column=3)
setgroupButton.grid(row=row, column=4)
row = row + 1
mainloop()
GUI = GUI()
GUI.Friends()

Keep a Frame in an other window Frame

My programm create a Frame with three panels in an horizontal boxsizer. A menu with "new window" item for create a seconde Frame. I give the seconde panel as parent of the seconde window. I wante the seconde Frame stays in the seconde panel area of my first frame.
if user move one of the two windows, the seconde stays in the panel screen area.
Do you know a way or something for that?
I tried a little something, but using is not very aesthetic.
and
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
#Widgets
panel_gch = wx.Panel(self,-1,size = (150,-1))
panel_gch.SetBackgroundColour('white')
self.panel=wx.Panel(self,-1,size=(300,400))
self.panel.SetBackgroundColour((200,230,200))
panel_drt = wx.Panel(self,-1,size = (150,-1))
panel_drt.SetBackgroundColour('white')
box = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(box)
#Add
box.Add(panel_gch,0,wx.EXPAND)
box.Add(self.panel,1,wx.EXPAND)
box.Add(panel_drt,0,wx.EXPAND)
#Menu
status=self.CreateStatusBar()
menubar=wx.MenuBar()
file_menu=wx.Menu()
ID_FILE_NEW = 1
file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
menubar.Append(file_menu,"File")
self.SetMenuBar(menubar)
#bind and layout
self.Bind(wx.EVT_MENU, self.get_new_window)
panel_gch.Layout()
self.panel.Layout()
panel_drt.Layout()
self.Layout()
def get_new_window(self,event): # create new window
self.new = NewWindow(self.panel,-1)
self.new.Show(True)
self.new.Bind(wx.EVT_MOVE,self.window2_on_move)
def window2_on_move(self,event): # Window2 must stay in
x, y = event.GetPosition()
v,w =self.panel.GetScreenPosition()
s,t = self.panel.GetClientSizeTuple()
if x < v:
self.new.Move((v,-1))
if y < w:
self.new.Move((-1,w))
if x+200 > v+s:
self.new.Move((v+s-200,-1))
if y+200 > w+t:
self.new.Move((-1,w+t-200))
class NewWindow(wx.MiniFrame):
def __init__(self,MainWindow,id):
wx.MiniFrame.__init__(self, MainWindow, id, 'New Window', size=(200,200),\
style = wx.MINIMIZE | wx.CAPTION | wx.CLOSE_BOX | wx.CLOSE_BOX)
self.CenterOnParent()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=MainWindow(parent=None,id=-1)
frame.Show()
app.MainLoop()
What you probably want is AUI. I personally recommend the wx.lib.agw.aui set rather than wx.aui as the former is pure Python and has had a LOT more recent work done on it. There are multiple examples in the wxPython demo package. You can also read about it here:
http://wxpython.org/Phoenix/docs/html/lib.agw.aui.framemanager.AuiManager.html
Thanks you very much Mike, exactly what I needed.
With wxpython I found This way:
the child stays in the panel area and it follows the window parent when moving.
import wx
class MainWindow(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
self.new = None
#Widgets
self.position = (0,0)
panel_gch = wx.Panel(self,-1,size = (150,-1))
panel_gch.SetBackgroundColour('white')
self.panel=wx.Panel(self,-1,size=(300,400))
self.panel.SetBackgroundColour((200,230,200))
panel_drt = wx.Panel(self,-1,size = (150,-1))
panel_drt.SetBackgroundColour('white')
box = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(box)
#Add
box.Add(panel_gch,0,wx.EXPAND)
box.Add(self.panel,1,wx.EXPAND)
box.Add(panel_drt,0,wx.EXPAND)
#Menu
status=self.CreateStatusBar()
menubar=wx.MenuBar()
file_menu=wx.Menu()
ID_FILE_NEW = 1
file_menu.Append(ID_FILE_NEW,"New Window","This is a new window")
menubar.Append(file_menu,"File")
self.SetMenuBar(menubar)
#bind and layout
self.Bind(wx.EVT_MENU, self.get_new_window)
panel_gch.Layout()
self.panel.Layout()
panel_drt.Layout()
self.Layout()
def get_new_window(self,event): # create new window
if self.new == None:
self.win_one_move = False
self.new = NewWindow(self.panel,-1)
self.new.Show(True)
self.new.Bind(wx.EVT_MOVE,self.window2_on_move)
self.Bind(wx.EVT_MOVE,self.window1_on_move)
v,w =self.GetPosition()
x, y = self.new.GetPosition()
self.get_windows_position((x-v),(y-w))
def get_windows_position(self,x,y):
self.position = (x,y)
print "check",self.position
def window2_on_move(self,event): # Window2 must stay in
if self.win_one_move == False:
x, y = event.GetPosition()
v,w =self.panel.GetScreenPosition()
s,t = self.panel.GetClientSizeTuple()
new_x,new_y = self.new.GetClientSizeTuple()
if x < v:
self.new.Move((v,-1))
if y < w:
self.new.Move((-1,w))
if x+new_x > v+s:
self.new.Move((v+s-new_x,-1))
if y+new_y > w+t:
self.new.Move((-1,w+t-new_y))
v,w =self.GetPosition()
x,y = self.new.GetPosition()
self.get_windows_position((x-v),(y-w))
if self.win_one_move == True:
self.win_one_move = False
def window1_on_move(self,event):
self.win_one_move = True
print "1 move"
x,y = self.GetPosition()
self.new.Move((x+self.position[0],y+self.position[1]))
print self.position
class NewWindow(wx.MiniFrame):
def __init__(self,MainWindow,id):
wx.MiniFrame.__init__(self, MainWindow, id, 'New Window', size=(200,200),\
style = wx.CAPTION | wx.CLOSE_BOX | wx.CLOSE_BOX)
self.CenterOnParent()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=MainWindow(parent=None,id=-1)
frame.Show()
app.MainLoop()
Can be use by another.Thanks

How to connect Fl_Text_Editor with output which is prodeced by a function?

I have a function which produces column of numbers:
def distancesplit(self):
img = np.asarray(Image.open("testtwo.tif").convert('L'))
img = 1 * (img < 127)
areasplit = np.split(img.ravel(), 24) # here we are splitting converted to 1D array
for i in areasplit:
area = (i == 0).sum()
print area
I want to output "area" result on Fl_Text_Editor widget. Here is the code I have now:
window = Fl_Window(100,100,400,400) # creating FLTK window
window.label(sys.argv[0])
button = Fl_Button(9,20,180,50) # making button class instance
button.label("Compute area")
button.callback(pixelarea) # connecting button to the function
button_two = Fl_Button(9,80,180,50)
button_two.label("Compute distances")
button_two.callback(distancesplit)
button_three = Fl_Button(9,140,180,50)
button_three.label("Compute features")
button_three.callback(distancetwo)
textedit = Fl_Text_Editor(9, 220, 180, 50)
textedit.buffer(self.textbuffer)
textedit.label("Text Editor")
textbuffer = Fl_Text_Buffer()
textbuffer.text("Code is written by the emotionally unstable alien who had survived space aircraft collision")
window.end() # code for running FLTK construction of widgets
window.show(len(sys.argv), sys.argv)
Fl.run() Thank you