I need a 'reset' push button to reset all other push buttons
1 次查看(过去 30 天)
显示 更早的评论
im making a tic tac toe game with a GUI and my 'new game' button isnt working!
my gui is made up of 9 push buttons for X's and O's and one New Game push button
i disabled all the buttons once theyre pressed so i have them enabled once i press the new game button but the X's and O's are still there.
any help is appreciated!
0 个评论
回答(1 个)
Dennis
2018-11-19
Without seeing your code it is hard to provide an answer. If the enabling works your probably did not set the string property of your buttons.
One possible solution might look like this:
handles.f=figure('Position',[200 200 800 800]);
for i=1:3
for k=1:3
handles.pb(i,k)=uicontrol('parent',handles.f,'style','pushbutton','Position',[i*200-200 k*200-200 200 200],'String',num2str(i));
end
end
set(handles.pb(:,:),'Callback',{@tictac_cb,handles});
handles.flag='X';
handles.newgame=uicontrol('style','pushbutton','position',[100 700 100 40],'String','New Game','Callback',{@newgame_cb,handles});
guidata(handles.f,handles)
function tictac_cb(hObj,~,handles)
handles=guidata(handles.f);
set(hObj,'String',handles.flag);
set(hObj,'Enable','Off')
if strcmp(handles.flag,'X')
handles.flag='O';
else
handles.flag='X';
end
guidata(handles.pb(1,1),handles)
end
function newgame_cb(~,~,handles)
set(handles.pb(:,:),'Enable','On');
set(handles.pb(:,:),'String','');
end
3 个评论
Dennis
2018-11-19
You enable the buttons, but you do not clear their strings. This can be done by setting their string property to '':
handles.button1.String='';
I'd also like to point out that putting handles in an array (see my example above) helps when you want to change multiple buttons. But even with GUIDE you could do it like this:
for i=1:9
h=handles.(sprintf('button%d',i));
set(h,'enable','on');
set(h,'string','');
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!