I am using python3 with Gtk3 and i need to basically remove some widgets from my Gtk.Window and replace them with other widgets when a Gtk.Button is clicked.I have tried using Gtk.ListBoxes with Gtk.ListBoxRows and so far i can remove all the Rows from the ListBox but when i try to add them back,essentially nothing happens.This is part of my code:
def left_view(self, box):
listbox = box
row0 = Gtk.ListBoxRow()
row0.set_halign(Gtk.Align.START)
listbox.add(row0)
#Adding HorizontalBox to place widgets
hbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.HORIZONTAL)
row0.add(hbox)
prod_name = Gtk.Label()
stock_count = Gtk.Label()
prod_name.set_text('Product Name')
stock_count.set_text('Stock ')
self.prod_name_input = Gtk.Entry()
self.stock_count_input = Gtk.Entry()
#Packaging 101
hbox.pack_start(prod_name, True, True, 0)
hbox.pack_start(self.prod_name_input, True, True, 0)
hbox.pack_start(stock_count, True, True, 0)
hbox.pack_start(self.stock_count_input, True, True, 0)
the function will be given a Gtk.ListBox as the argument.And what i have so far when trying to remove() and add() things to the listbox is:
def remover(self,widget):
vbox = widget.get_parent()
base = vbox.get_parent()
children = base.get_children()
listbox = children[1]
for row in listbox.get_children():
listbox.remove(row)
with open('product_info.json', 'r') as d:
data = d.read()
j = json.loads(data)
d.close()
self.keys = list(j.keys())
row0 = Gtk.ListBoxRow()
listbox.add_child(row0)
scrollwindow = Gtk.ScrolledWindow()
scrollwindow.set_hexpand(True)
scrollwindow.set_vexpand(True)
row0.add_child(scrollwindow)
tview = Gtk.TextView()
scrollwindow.add(tview)
textbuffer = tview.get_buffer()
for item in range(0, len(self.keys)):
textbuffer.insert_at_cursor(' %s\t\t %s \n' %(item, self.keys[item]))
By the way,If there is a simpler way of just replacing widgets on the go in PyGObject,please do let me know coz most of the answers that i saw on the related questions where totally useless
I was baffled by this, too. It turned out the created widgets must be explicitly shown with show() or show_all(). In your case:
row0.show_all()
as the last statement.
Related
Context:
I click on a button -> a WindowDialogue appears -> there are 2 LineEdit Nodes and a button
Supposed procedure:
I fill these LineEdits, press the button, and the function tied to the button receives the texts from the LineEdits (and creates a dictionary key-value pair).
Problem:
The texts from the LineEdits do not pass down to the function (through connect()).
I tried passing the text directly with LineEdit.text. I tried writing it in a different variable and passing the variable. The debug shows the function is getting executed (no problem with that), but the strings passed from the LineEdits are always empty. Somehow, it worked for some time when I explicitly pressed ENTER after filling the LineEdits. But after I tried to fix the aforementioned problem, it stopped working altogether.
P.S. Everything I mentioned is created dynamically through the code, if that matters.
Code:
#Declaring the variables
var temp_text_key = ""
var temp_text_value = ""
#Function to show up a dialogue window with LineEdits and Confirm button
func _on_add_new_property(dict: Dictionary):
temporal_window_dialogue = WindowDialog.new()
temporal_window_dialogue.rect_min_size = Vector2(410,90)
temporal_window_dialogue.resizable = true
temporal_window_dialogue.popup_exclusive = true
temporal_window_dialogue.window_title = "Adding new property to %s" % get_dict_name_by_id(professions, dict["id"])
var le_key = LineEdit.new()
le_key.placeholder_text = "Name of the property"
le_key.hint_tooltip = "Name of the property"
le_key.connect("text_changed", self, "_on_add_new_prop_text_entered", ["key"])
var le_value = LineEdit.new()
le_value.expand_to_text_length = true
le_value.placeholder_text = "Value of the property"
le_value.set("custom_constants/minimum_spaces", 36)
le_value.hint_tooltip = "Value of the property"
le_value.connect("text_changed", self, "_on_add_new_prop_text_entered", ["value"])
var lab = Label.new()
lab.text = "Should the Value become a Dictionary?"
var check_bt = CheckBox.new()
var vbox = VBoxContainer.new()
var grid_container = GridContainer.new()
grid_container.columns = 2
var accept_bt = Button.new()
accept_bt.text = "Confirm"
temporal_window_dialogue.add_child(vbox)
vbox.add_child(grid_container)
grid_container.add_child(le_key)
grid_container.add_child(le_value)
grid_container.add_child(lab)
grid_container.add_child(check_bt)
vbox.add_child(accept_bt)
grid_container.set("custom_constants/vseparation", 5)
grid_container.set("custom_constants/hseparation", 5)
check_bt.connect("pressed", self, "_on_check_dict_pressed")
popup_window.add_child(temporal_window_dialogue)
temporal_window_dialogue.connect("popup_hide", self, "_on_close_specific_codegenerated_popup", [temporal_window_dialogue])
temporal_window_dialogue.popup_centered()
accept_bt.connect("pressed", self, "_on_add_new_property_confirmation", [dict, temp_text_key, temp_text_value])
#Function to create a new key/value pair in a dictionary
func _on_add_new_property_confirmation(dict: Dictionary, prop_name: String, prop_value: String):
if prop_name == "" or prop_value == "":
send_message_to_console("Neither of the two can be empty", 3)
return
if add_dictionary_flag:
dict[prop_name] = {
prop_value: {
"id": get_free_local_id_for_dict(dict)
}
}
elif !add_dictionary_flag:
if keywords_for_dictionaries.has(prop_name):
send_message_to_console("Only Dictionary items can become %s" % prop_name)
dict[prop_name] = prop_value
temp_text_key = ""
temp_text_value = ""
#Optional function to write the text from LineEdits to the variables
func _on_add_new_prop_text_entered(new_text, key_or_value):
if key_or_value == "key":
temp_text_key = new_text
elif key_or_value == "value":
temp_text_value = new_text
You mentioned using 'LineEdit.text', but have you tried the following?
var text = LineEdit.get_text()
If this doesn't help, please update your original post to include code examples.
So I'm trying to write some program to convert html to pptx using Python.
I'm using some code to parse through the file and then format text at run level.
I am using 2 textframes on a blank slide to work, the first textframe is used for title and 2nd one is used for rendering the body of html.
But no matter what I do (Paragraph.font.name or run.font.name), I am always getting Arial font in my 2nd textframe.
I've tried to update the fontnames by looping over tf.paragraphs and also by looping over all runs (paragraph.runs) nothing seems to work.
Here is my function which I use for setting the font
def setFont(font,tags,fontStyle,size):
font.language_id = MSO_LANGUAGE_ID.HEBREW
font.size = Pt(size)
font.name = fontStyle
for tag in tags:
if tag == 'b':
font.bold = True
elif tag == 'i':
font.italic = True
elif tag == 'u':
font.underline = True
return font
and here is my main calling function (I've removed some other calls which are not directly related to python-pptx)
def makeSlide(prs,title,body,fontStyle,titleSize,bodySize):
#Add title Slide
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
width = height = Inches(8)
txBox = slide.shapes.add_textbox(Inches(1), Inches(0.5), Inches(8), Inches(1))
tf = txBox.text_frame
tf.text = title
tf.paragraphs[0].font.size=Pt(titleSize)
tf.paragraphs[0].font.bold = True
tf.paragraphs[0].font.color.rgb = RGBColor(255,0,0)
tf.paragraphs[0].font.name = fontStyle
tf.paragraphs[0].alignment = PP_ALIGN.CENTER
styles = MY_CUSTOM_HTML_BODY_PARSER() #Removed this part from here, its just a parser which gives output in a desired format
#Start 2nd textbox for body
txBox2 = slide.shapes.add_textbox(Inches(0.5), Inches(1.7), Inches(9), Inches(5.5))
tf = txBox2.text_frame
tf.clear()
tf.word_wrap = True
tf.paragraphs[0].text = ''
tf.paragraphs[0].font.name = fontStyle
paragraph = tf.add_paragraph()
paragraph.alignment = alignment
#Loop over styles (Tag info and render pptx accordingly)
new_line = True
level = 0
number = 0
for _ in styles:
#Can ignore this part, I'm just checking to see if I have to go on a new line or not
if _ == '\n':
paragraph = tf.add_paragraph()
paragraph.alignment = alignment
new_line = True
else:
tags = _[1]
#Can also ignore this, I'm checking to see if I have to be on next level in lists or not
#(I use custom bullet function, since python-pptx doesn't have a function for numbered bullets.
if _[1].count('ul') > 1 or _[1].count('ol') > 1 or _[1].count('ul') + _[1].count('ol') > 1:
level = 1
if new_line:
paragraph.level = level
level = 0
else:
paragraph = tf.add_paragraph()
paragraph.alignment = alignment
paragraph.level = level
level = 0
new_line = True
if new_line:
run = paragraph.add_run()
#Custom bullet function as u can see I send in fontStyle here and apply style inside function
run,number = addBullets(run,tags,bodySize-3,number,fontStyle)
new_line = False
run = paragraph.add_run()
run.text = parser.words_styles[_[0]][0]
font = run.font
#SetFont function Defined above
font = setFont(font,tags,fontStyle,bodySize)
new_line = False
for paragraph in tf.paragraphs:
paragraph.font.name = fontStyle
for run in paragraph.runs:
if run.font.name != fontStyle:
print(run.text)
#return slide
return slide
I'm not quite sure where I am doing wrong, would appreciate any help, I can clean up code more if that's needed.
I'm trying to call a popup and the have the user input text. After hitting the save button inside my popup it updates a new widget.
I have almost everything working but I don't know how to properly call a function inside the same class.
I have two functions inside of my
'class Trackers(Screen):'
how can I make a button that 'on_press' command will trigger another function?
The line of code I need to be fixed is this:
okay = Button(text='Save', on_press=self.addit(str(text_input), num), on_release=pop.dismiss)
Thank you so much.
''''
def addWidget(self):
num = '0'
box = BoxLayout(orientation= 'vertical', padding=40, spacing=5)
pop = Popup(title='Tracker: details', content=box, size_hint=(None,None), size=(300,300))
text_input = TextInput(hint_text='Add Trackers', multiline=False,)
okay = Button(text='Save', on_press=self.addit(str(text_input), num), on_release=pop.dismiss)
box.add_widget(text_input)
box.add_widget(okay)
pop.open()
def addit(self, text_input, num, *args):
if text_input not in self.storage.keys():
self.ids.track.add_widget(Tracker(text=text_input, number=num, data=self.storage))
self.storage[text_input] = '0'
text_input = ''
self.saveData()
else:
box = BoxLayout(orientation= 'vertical', padding=40, spacing=5)
pop = Popup(title=f'Opps: "{text_input}" is already listed below', content=box, size_hint=(None,None), size=(300,180))
try_again = Button(text='Try Again', on_release=pop.dismiss)
box.add_widget(try_again)
pop.open()
''''
I'm trying to use checkmouse in order to undraw something from my window. When someone clicks the button it should undraw the text and write something else. I'm using checkMouse and getX() and getY() to do this but i keep receiving this error that states:
File "C:\Users\User\Documents\python\project2.py", line 71, in panel
if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
AttributeError: 'NoneType' object has no attribute 'getX'
this code that i have done so far is as follows:
from graphics import *
#creating the game panel window
def panel():
#grey window, with coordinates flipped, with banners etc
win = GraphWin("Start Panel", 300,200)
win.setCoords(0,0,300,200)
win.setBackground("light grey")
#drawing the BoilerMazer banner with text
boilermazer = Rectangle(Point(0,200),Point(300,160))
boilermazer.setFill("white")
boilermazer.draw(win)
#text inside
banner1 = Text(Point(150,180),"BoilerMazer")
banner1.setStyle("bold")
banner1.setSize(20)
banner1.draw(win)
#initial game panel is going to have two buttons and a top scores object
#top score "screen"
toprec = Rectangle(Point(60,140),Point(240,50))
toprec.setFill("white")
toprec.draw(win)
#text inside toprec
topscores = Text(Point(150,130),"TOP SCORES")
topscores.setSize(8)
topscores.draw(win)
border = Text(Point(150,120),"======")
border.draw(win)
bigmac = Text(Point(150,110),"Big Mac 21")
bigmac.setSize(8)
bigmac.draw(win)
tt = Text(Point(150,90),"T.T 23")
tt.setSize(8)
tt.draw(win)
cshell = Text(Point(150,75),"C-Shell 25")
cshell.setSize(8)
cshell.draw(win)
macmac = Text(Point(150,55),"MacMac 27")
macmac.setSize(8)
macmac.draw(win)
#new player button that will eventually be clicked
new1 = Point(90,35)
new2 = Point(210,0)
newrec = Rectangle(new1,new2)
newrec.setFill("chartreuse2")
newrec.draw(win)
#new player button text
newplayer = Text(Point(150,18),"NEW PLAYER")
newplayer.draw(win)
#reset button
resetrec = Rectangle(Point(240,35),Point(300,0))
resetrec.setFill("red")
resetrec.draw(win)
#resettext
reset = Text(Point(270,18),"RESET")
reset.draw(win)
#secondary panel window is the game panel after they click new player
#set up points that we check between for the new player button first
#setting up the checkmouse
clicknew = win.checkMouse()
if (clicknew.getX()>90 and clicknew.getX()<210) and (clicknew.getY()>35 and clicknew.getY() < 0):
newplayer.undraw()
you can find the graphics window here:http://mcsp.wartburg.edu/zelle/python/graphics.py
I don't understand what I'm doing wrong, is there some other method that I'm supposed to be using? Thanks for your help
According to the docs, checkMouse() returns None if no mouse click has been detected priorly. So that seems to be the case.
You could put a loop around the call to checkMouse and keep checking if clicknew is not None and only in that case go on in your program. But maybe there's a better way...
UPDATE:
Example:
while True:
clicknew = win.getMouse()
if clicknew:
break
else:
time.sleep(0.1) # avoid busy waiting
# clicknew is set now => use it
I am trying to create a small window that shows up in the corner of your screen. The window will have a keyboard on it. I want to be able to make the computer think that you pressed a button on your keyboard when a button in the window is pressed. This is the code I have so far:
from tkinter import *
top = Tk()
frame1 = Frame(top)
btn = Button(frame1,text="q",command= 'print("q",end="")')
btn2 = Button(frame1,text="w",command= 'print("w",end="")')
btn3 = Button(frame1,text="e",command= 'print("e",end="")')
btn4 = Button(frame1,text="r",command= 'print("r",end="")')
btn5 = Button(frame1,text="t",command= 'print("t",end="")')
btn6 = Button(frame1,text="y",command= 'print("y",end="")')
btn7 = Button(frame1,text="u",command= 'print("u",end="")')
btn8 = Button(frame1,text="i",command= 'print("i",end="")')
btn9 = Button(frame1,text="o",command= 'print("o",end="")')
btn10 = Button(frame1,text="p",command='print("p",end="")')
frame2 = Frame(top)
btn11 = Button(frame2,text="a",command='print("a",end="")')
btn12 = Button(frame2,text="s",command='print("s",end="")')
btn13 = Button(frame2,text="d",command='print("d",end="")')
btn14 = Button(frame2,text="f",command='print("f",end="")')
btn15 = Button(frame2,text="g",command='print("g",end="")')
btn16 = Button(frame2,text="h",command='print("h",end="")')
btn17 = Button(frame2,text="j",command='print("j",end="")')
btn18 = Button(frame2,text="k",command='print("k",end="")')
btn19 = Button(frame2,text="l",command='print("l",end="")')
frame3 = Frame(top)
btn20 = Button(frame3,text="z",command='print("z",end="")')
btn21 = Button(frame3,text="x",command='print("x",end="")')
btn22 = Button(frame3,text="c",command='print("c",end="")')
btn23 = Button(frame3,text="v",command='print("v",end="")')
btn24 = Button(frame3,text="b",command='print("b",end="")')
btn25 = Button(frame3,text="n",command='print("n",end="")')
btn26 = Button(frame3,text="m",command='print("m",end="")')
btnArr1 = [btn,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10]
btnArr2 = [btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19]
btnArr3 = [btn20,btn21,btn22,btn23,btn24,btn25,btn26]
for x in range(0,len(btnArr1),1):
btnArr1[x].pack(side=LEFT)
frame1.pack()
for x in range(len(btnArr2)-1,-1,-1):
btnArr2[x].pack(side=RIGHT)
frame2.pack()
for x in range(len(btnArr3)-1,-1,-1):
btnArr3[x].pack(side=RIGHT)
frame3.pack()
top.mainloop()
When I run it, it does nothing. I tried using the plain code without the single quotes, and I tried using an exec() statement in the command parameter. Both tries failed.
I am using python 3.6.2
If anyone has any idea why this is, I would love to hear what you have to say. Thank you for your time!
The command= option requires a function or other callable object - a string does not qualify. You could try command=lambda: print("q",end="") to create such a function for each button.