Disable and enable radiobuttons from another radiobutton in PyQt4 Python - python

I have 3 radiobutton groups in forms of Yes or No. I want to disable the last 2 radiobutton groups if the first radiobutton is "No" and enable them when the first radiobutton is "Yes". I can make the enabled or disabled by default but toggling Yes/No from the first group doesn't change the result in the second and third group.
Here is a summary of my code:
self.yes_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup1.setObjectName(_fromUtf8("yes_radioButtonGroup1"))
self.no_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup1.setObjectName(_fromUtf8("no_radioButtonGroup1"))
self.radioButtonGroup1= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup1.setObjectName(_fromUtf8("radioButtonGroup1"))
self.radioButtonGroup1.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup1.addButton(self.no_radioButtonGroup1)
self.yes_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup2.setObjectName(_fromUtf8("yes_radioButtonGroup2"))
self.no_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup2.setObjectName(_fromUtf8("no_radioButtonGroup2"))
self.radioButtonGroup2= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup2.setObjectName(_fromUtf8("radioButtonGroup2"))
self.radioButtonGroup2.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup2.addButton(self.no_radioButtonGroup1)
self.yes_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup3.setObjectName(_fromUtf8("yes_radioButtonGroup3"))
self.no_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup3.setObjectName(_fromUtf8("no_radioButtonGroup3"))
self.radioButtonGroup3= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup3.setObjectName(_fromUtf8("radioButtonGroup3"))
self.radioButtonGroup3.addButton(self.yes_radioButtonGroup3)
self.radioButtonGroup3.addButton(self.no_radioButtonGroup3)
if self.yes_radioButtonGroup1.isChecked() == True:
self.yes_radioButtonGroup2.setEnabled(True)
self.no_radioButtonGroup2.setEnabled(True)
self.yes_radioButtonGroup3.setEnabled(True)
self.no_radioButtonGroup3.setEnabled(True)
elif self.no_radioButtonGroup1.isChecked() == True:
self.yes_radioButtonGroup2.setEnabled(False)
self.no_radioButtonGroup2.setEnabled(False)
self.yes_radioButtonGroup3.setEnabled(False)
self.no_radioButtonGroup3.setEnabled(False)
I was wondering if anybody knows how to dynamically enable or disable the last two radiobutton group when values from the first radiobutton changes?

Since the buttons are exclusive enough to monitor one of them, so the appropriate signal is toggled() that sends the information if the button is checked or not.
self.yes_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup1.setObjectName(_fromUtf8("yes_radioButtonGroup1"))
self.no_radioButtonGroup1 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup1.setObjectName(_fromUtf8("no_radioButtonGroup1"))
self.radioButtonGroup1= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup1.setObjectName(_fromUtf8("radioButtonGroup1"))
self.radioButtonGroup1.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup1.addButton(self.no_radioButtonGroup1)
self.yes_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup2.setObjectName(_fromUtf8("yes_radioButtonGroup2"))
self.no_radioButtonGroup2 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup2.setObjectName(_fromUtf8("no_radioButtonGroup2"))
self.radioButtonGroup2= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup2.setObjectName(_fromUtf8("radioButtonGroup2"))
self.radioButtonGroup2.addButton(self.yes_radioButtonGroup1)
self.radioButtonGroup2.addButton(self.no_radioButtonGroup1)
self.yes_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
self.yes_radioButtonGroup3.setObjectName(_fromUtf8("yes_radioButtonGroup3"))
self.no_radioButtonGroup3 = QtGui.QRadioButton(self.centralwidget)
self.no_radioButtonGroup3.setObjectName(_fromUtf8("no_radioButtonGroup3"))
self.radioButtonGroup3= QtGui.QButtonGroup(MainWindow)
self.radioButtonGroup3.setObjectName(_fromUtf8("radioButtonGroup3"))
self.radioButtonGroup3.addButton(self.yes_radioButtonGroup3)
self.radioButtonGroup3.addButton(self.no_radioButtonGroup3)
self.yes_radioButtonGroup1.toggled(self.on_yes_checked)
# set initial state
self.on_yes_checked(self.yes_radioButtonGroup1.isChecked())
def on_yes_checked(self, checked):
self.yes_radioButtonGroup2.setEnabled(checked)
self.no_radioButtonGroup2.setEnabled(checked)
self.yes_radioButtonGroup3.setEnabled(checked)
self.no_radioButtonGroup3.setEnabled(checked)

Related

Problem with Messagebox it runs only that

i dont know why i cannot put all of my code
i created a function w3 and i add some checkbuttons the problem is that if the user clicks "< Υποβολή >" the function listokouti only recognizes the messagebox and its like i never put elif,can anyone help?
i1=IntVar()
i2=IntVar()
i3=IntVar()
i4=IntVar()
i5=IntVar()
#i6=IntVar()
#i7=IntVar()
# checkbuttons
check1 = tk.Checkbutton(rain,text="Τίτλος",font=('',13),variable=i1,onvalue=1,offvalue=0).pack(expand=1,anchor="nw")
check2 = tk.Checkbutton(rain,text="Tίτλος(Μετάφραση)",font=('',13),variable=i2,onvalue=1,offvalue=0).pack(expand=1,anchor="nw")
check3 = tk.Checkbutton(rain,text="Συγγραφέας",font=('',13),variable=i3,onvalue=1,offvalue=0).pack(expand=1,anchor="nw")
check4 = tk.Checkbutton(rain,text="Λέξεις κλειδιά",font=('',13),variable=i4,onvalue=1,offvalue=0).pack(expand=1,anchor="nw")
check5 = tk.Checkbutton(rain,text="Λέξεις κλειδιά(Μετάφραση)",font=('',13),variable=i5,onvalue=1,offvalue=0).pack(expand=1,anchor="nw")
def listokouti():
if i1.get()==0 and i2.get()==0 and i3.get()==0 and i4.get()==0 and i5.get()==0 :
vsause=messagebox.showwarning("team.33","Παρακαλώ επιλέξτε φίλτρα")
elif i1.get()==1 or i2.get()==1 or i3.get()==1 or i4.get()==1 or i5.get()==1 :
money=tk.Tk()
money.title("team.33")
money.iconbitmap('C:\Program Files (x86)\icon.ico')
# money.config(bg="#000000")
frame=tk.Frame(money)
scrollarw1=ttk.Scrollbar(frame,orient=VERTICAL)
my_listbox= tk.Listbox(frame,yscrollcommand=scrollarw1.set,width=50)
scrollarw1.config(command=my_listbox.yview)
scrollarw1.pack(side=RIGHT,fill= Y)
scrollarw2=ttk.Scrollbar(frame,orient=HORIZONTAL)
scrollarw2.config(command=my_listbox.xview)
scrollarw2.pack(side=BOTTOM,fill= X)
frame.pack()
my_listbox.pack(pady=20)
# to item einai akyro to evala mono gia na leitoyrgei to scrollbar
for item in range(1,101):
my_listbox.insert(END,item)
butt =tk.Button(money,text="< Υποβολή >",font=("",10,'bold'),command=ergasia).pack()
money.mainloop()
b1 = tk.Button(rain,text="< Υποβολή >",font=("",10,"bold"),command= listokouti).pack(expand=1)
rain.mainloop()
w3()

How to to save one document based on an if statement in Python?

I am trying to save a document based on a if statement.
Here I am creating radiobuttons:
info = ["Option 1", "Option 2", "Option 3"]
vars = []
for idx,i in enumerate(info):
var = IntVar(value=0)
vars.append(var)
lblOption = Label(main,text=i)
btnYes = Radiobutton(main, text="Yes", variable=var, value=2)
btnNo = Radiobutton(main, text="No", variable=var, value=1)
btnNa = Radiobutton(main, text="N/A", variable=var,value=0)
lblOption.grid(column=4,row=idx, sticky = W)
btnYes.grid(column=1,row=idx)
btnNo.grid(column=2,row=idx)
btnNa.grid(column=3,row=idx)
Here I am creating a document
document = Document()
#add table
table = document.add_table(1, 4)
#style table
table.style = 'Table Grid'
#populate header row
heading_cells = table.rows[0].cells
heading_cells[0].text = "Options"
heading_cells[1].text = "Yes"
heading_cells[2].text = "No"
heading_cells[3].text = "N/a"
for idx, item in enumerate(vars):
cells = table.add_row().cells
cells[0].text = info[idx] # gets the option name
val = item.get() #radiobutton value
if val == 2: # checks if yes
cells[1].text = "*"
elif val == 1: # checks if no
cells[2].text = "*"
elif val == 0: # checks if N/A
cells[3].text = "*"
#save doc
document.save("test.docx")
Work behind the scenes:
Out of the 3 radio-button Yes, No, N/a.. Only one can be chosen.
Next, when pressed a button save.. it creates a table in docx, Options is in row 0 appending down along with selected values of Yes, no & N/a.
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
My Problem:
I can simply press save and it saves the file as test.docx.
Now, I am trying to figure out how to save the file as Failed.docx
The Failed.docx will only be created if one or more out of all the options has a no value selected.
As an example below, this would be saved as Test.docx, because not a single Option has a no value selected:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
An example below, this would be saved as Failed.docx, because no option has been selected for one of the options on the left.
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
Here is what I have tried so far:
for x in cells[2].text:
if "*" in x:
print("True")
else:
print("False")
This detects * within the cell[2] (This is row 2 linked to No value).
And if a 'no' value has been selected it prints out true but also prints out false
As an example:
Options Yes No N/a
Option 1 *
Option 2 *
Option 3 *
Output of the for loop :
False
True
False
But if it detects False and True both files will be saved. I am totally confused where to go from here..
Question: The 'Failed.docx' will only be created if one or more out of all the options has a no value selected.
This can be rephrased to:
if any option has NO
You have build a list of Boolean from the condition value == NO,
like [False, True, False]
Built-in - any
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False.
YES = 2; NO = 1; NA = 0
print(vars)
if any([v.get() == NO for v in vars]):
print('Failed.docx')
else:
print('test.docx')
Output:
[2, 0, 2]
test.docx
[2, 1, 2]
Failed.docx
Try the following:
for x in cells[2].text:
if "*" in x:
print("Failed.docx")
elif "*" not in x:
print("Test.docx")
this checks if any "*" is inside your no row, if it exists save as Failed.docx if not save as Test.docx

Maya phone number query

I'm working on a script that needs a phone number queried from an tex field or int field for maya. I using python and I can't seem to find anything that works. Can you help?
Thanks
TED
Ok This is the mess that I came up with.
def makeTui():
if(cmds.window('window2',q=1,ex=1)):cmds.deleteUI('window2')
cmds.window('window2',menuBar=1)
cmds.formLayout('formLayout1')
cmds.text(label='Phone Number')
num = cmds.intField('textField4',width=100,height=20,changeCommand = num_callback)
cmds.text('text95',label='Service Provider')
cmds.optionMenu('optionMenu1')
cmds.menuItem(label='AT&T')
cmds.menuItem(label='Verizon')
cmds.menuItem(label='Sprint')
cmds.menuItem(label='Cricket')
cmds.menuItem(label='Tmobil')
cmds.iconTextButton('iconTextButton45',parent='formLayout1',image='render.png',command='num_callback()')
cmds.formLayout('formLayout1',e=1,attachForm=[['textField4', 'left', 100], ['text95', 'top', 30], ['optionMenu1', 'top', 30], ['optionMenu1', 'left', 100], ['iconTextButton45', 'left', 100], ['iconTextButton45', 'top', 60]])
cmds.showWindow('window2')
makeTui()
def num_callback():
print cmds.intField(num, q=True, value=True)
You want to define your callback function in the same scope as the UI items - that saves a lot of work in trying to remember the names of the widgets that you need to work on.
def create_ui():
window = cmds.window()
column = cmds.columnLayout(adj=True)
# three items arranged horizontally
row = cmds.rowLayout(nc = 3)
numfield = cmds.textFieldGrp(label = 'phone number')
cmds.text("provider")
provider = cmds.optionMenu()
for p in ('AT&T', 'Verizon', 'Sprint', 'T-Mobile'):
cmds.menuItem(label = p)
cmds.setParent("..")
# define this function here so it knows the widgets for
# the text field and the option menu
def render_button():
phoneno = cmds.textFieldGrp(numfield, q=True, text=True)
# remove any punctuation:
digits =[int(c) for c in phoneno if c.isdigit()]
providername = cmds.optionMenu(provider, q=True, v=True)
print digits, providername
cmds.iconTextButton(image='render.png', c = render_button)
cmds.showWindow(window)
Here the callback function knows the 'names' of the field for entering numbers and the optionmenu for picking providers. The actual work would go into the render_buttons() function. I used a text field, incidentally, since many people will expect to type things like 1(555)111-2222 and an intField wont' allow it.

How can I group RadioMenuItem? (PyGtk)

That's the question.
I'm making an indicator for Ubuntu, all works fine, but... When I try to set two "RadioMenuItem" they are totally independent. I can check both.
The items:
item_first = gtk.RadioMenuItem('First Radio')
item_first.connect('activate', first_radio)
menu.append(item_first)
item_second = gtk.RadioMenuItem('Second Radio')
item_second.connect('activate', second_radio)
menu.append(item_second)
Should I use a container or something like this? Please, help me.
Try this,
self.menu = Gtk.Menu()
self.menu_items = list("First Radio","Second Radio")
group = []
for i in range(1,5):
menu_item = Gtk.RadioMenuItem.new_with_label(group, str(i))
group = menu_item.get_group()
self.menu_items[i] = menu_item
self.menu.append(menu_item)
menu_item.connect("activate", self.on_menu_select, i)
menu_item.show()
self.menu_items[2].set_active(True)
That's an old one, I know. However the solution is as following:
item_first = gtk.RadioMenuItem('First Radio')
item_first.connect('activate', first_radio)
menu.append(item_first)
item_second = gtk.RadioMenuItem('Second Radio', group=item_first) # <-- note group
item_second.connect('activate', second_radio)
menu.append(item_second)

graphics in python to make a task manger

I am designing a task manager for a month using graphics in python. I have created the calendar for the month of July. I would like to display the tasks for a given date, add new tasks, remove/delete tasks and save the changes to a file.
The maximum number of to do tasks for any day is 5. Initially, the list of tasks are read from a file. Each row in the file contains the date followed by a list of semi-colon separated tasks for that date. The following shows an example file:
1; Go to class;Book Train Ticket
2; Call Home
3; Wash the Clothes
7; Submit the Assignment Online
8; Give Assignment Demo;Prepare for Major
10; Take the Exam
11; Go Home
To the right of calendar, there is a textbox to enter a new task to the list of to-do tasks for the date currently being displayed. After entering the new task in the textbox, I need to click on the ’ok’ button to add it to the list. As soon as the ’ok’ button is pressed, the list of tasks being displayed should be refreshed to reflect the addition of the new task. The textbox should be cleared after the changes have been incorporated into the task list. Here's my code:
from graphics import *
win =GraphWin("Task Manager",800,800)
head = Text(Point(130,15),"July2014")
head.setSize(16)
head.draw(win)
add = Text(Point(440,28),"Add Task :")
add.setSize(17)
add.draw(win)
adde = Entry(Point(560,28),20)
adde.draw(win)
rem = Text(Point(440,78),"Remove Task:")
rem.setSize(17)
rem.draw(win)
reme = Entry(Point(570,78),20)
reme.draw(win)
addr = Rectangle(Point(680,15),Point(640,45))
addr.setFill("yellow")
addr.draw(win)
okt = Text(Point(660,30),"ok")
okt.setSize(16)
okt.draw(win)
remr = Rectangle(Point(690,60),Point(650,90))
remr.setFill("green")
remr.draw(win)
okt = Text(Point(670,75),"ok")
okt.setSize(16)
okt.draw(win)
savr =Rectangle(Point(440,120),Point(540,150))
savr.setFill("orange")
savr.draw(win)
savt = Text(Point(490,135),"Save to file")
savt.setSize(15)
savt.draw(win)
exir = Rectangle(Point(590,120),Point(650,150))
exir.setFill("red")
exir.draw(win)
exis = Text(Point(620,135),"exit")
exis.setSize(15)
exis.draw(win)
def isInside(p,rect):
rectP1 = rect.getP1();
rectP2 = rect.getP2();
if(p.getX() >= rectP1.getX() and p.getX() <= rectP2.getX() and
p.getY() >= rectP1.getY() and p.getY() <= rectP2.getY()):
return True;
else:
return False;
dtext = Text(Point(180,350),"no date selected yet")
dtext.setSize(16)
dtext.draw(win)
def rect(a):
squ = Rectangle(Point(20+((a-1)%7)*40,70+(((a-1)//7)-1)*30),Point(20+(((a- 1)%7)+1)*40,70+((a-1)//7)*30))
return squ
for i in range(1,43):
rect(i).draw(win)
l =['sun','mon','tue','wed','thu','fri','sat']
def elem(a):
p = Point(40+((a-1)%7)*40,85+(((a-1)//7)-1)*30)
sti = Text(p,l[a-1])
sti.setSize(15)
return sti
for i in range(1,8):
elem(i).draw(win)
def num(a):
p = Point(40+((a-1)%7)*40,85+(((a-1)//7)-1)*30)
b = Text(p,a-9)
return b
for j in range(10,41):
num(j).draw(win)
id = Text(Point(50,400),"id")
id.setSize(17)
id.draw(win)
task = Text(Point(200,400),"task")
task.setSize(17)
task.draw(win)
while 9 < a< 41:
inputP = win.getMouse()
if isInside(inputP,rect(a)):
dtext.setText("tasks on July"+rect(a).getText()+"are")
else:
dtext.setText("wrong")
win.getMouse()
win.close()

Categories

Resources