TCL:单击检查按钮

时间:2016-10-21 10:07:49

标签: tcl tk

我正在尝试设置一系列检查按钮,点击这些按钮会将链接到这些按钮的变量值设置为1。 “运行”按钮仅运行选中了“检查”按钮的检查。 全局变量值例如:单击按钮时不会更新check_H1。

#!/usr/local/bin/wish

package require Tk

frame .top -width 50 -height 30 -borderwidth 5 -padx 5 -pady 5 -relief raised
checkbutton .top.c1 -text H1 -variable "check_H1" -command {set_h1}
checkbutton .top.c2 -text H2 -variable "check_H2" -command {set_h2} 
checkbutton .top.c3 -text H3 -variable "check_H3" -command {set_h3}
checkbutton .top.c4 -text H4 -variable "check_H4" -command {set_h4}
checkbutton .top.c5 -text H5 -variable "check_H5" -command {set_h5}
checkbutton .top.c6 -text H6 -variable "check_H6" -command {set_h6}

button .top.b1 -text "RUN" -command [list select $check_H1 $check_H2 $check_H3 $check_H4 $check_H5 $check_H6] 
grid .top
grid .top.c1 -row 2 -column 2 
grid .top.c2 -row 2 -column 3 
grid .top.c3 -row 3 -column 2 
grid .top.c4 -row 3 -column 3 
grid .top.c5 -row 4 -column 2 
grid .top.c6 -row 4 -column 3 
grid .top.b1 -row 5 -column 5

proc select {check_H1 check_H2 check_H3 check_H4 check_H5 check_H6} {
    #upvar check_H1 check_H2 check_H3 sa3 check_H4 sa4 check_H5 sa5 check_H6 sa6
    puts "Value of H1 is $check_H1\n"
    puts $check_H2
    puts $check_H3
    puts $check_H4
    puts $check_H5
    puts $check_H6

    if {$check_H1 == 1} {
        run_h1
    }
    if {$check_H2 ==1} {
        run_h2
    }
    if {$check_H3 ==1} {
        run_h3
    }
    if {$check_H4 ==1} {
        run_h4
    }
    if {$check_H5 ==1} {
        run_h5
    }
    if {$check_H6 ==1} {
        run_h6
    }
}

proc set_h1 {} {
    global check_H1
    set check_H1 1
    puts $check_H1
    puts "H1 is set\n"
}
proc set_h2 {} {
    global check_H2
    set check_H2 1
    puts "H2 is set\n"
}
proc set_h3 {} {
    global check_H3
    set check_H3 1
    puts "H3 is set\n"
}
proc set_h4 {} {
    set check_H4 1
    puts "H4 is set\n"
}
proc set_h5 {} {
    set check_H5 1
    puts "H5 is set\n"
}
proc set_h6 {} {
    set check_H6 1
    puts "H6 is set\n"
}

proc run_h1 {} {
    global check_H1 
    puts "this loop is for H1\n"
}
proc run_h2 {} {
    global check_H2
    puts "this loop is for H2\n"
}

1 个答案:

答案 0 :(得分:2)

在运行脚本时,感觉有很多东西不能像我想的那样工作。我改变了一些东西,并从按钮命令中删除了变量,因为变量被传递给要在开始时运行的命令而不是按下按钮的位置。

我还删除了所有public class HomeActivity2 extends AppCompatActivity { public ViewPagerAdapter adapter; private Toolbar toolbar; private TabLayout tabLayout; private NonSwipeableViewPager viewPager; public ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>(); public ArrayList<MenuItem> allMenuItemsList = new ArrayList<MenuItem>(); ArrayList<MenuItemCategory> allMenuItemCategoryList = new ArrayList<MenuItemCategory>(); //Getters public NonSwipeableViewPager getViewPager() { return viewPager; } public ArrayList<String> getMappingOfCategoryWithIndex() { return mappingOfCategoryWithIndex; } public ArrayList<MenuItem> getAllMenuItemsList() { return allMenuItemsList; } public TabLayout getTabLayout() { return tabLayout; } public ArrayList<MenuItemCategory> getAllMenuItemCategoryList() {return allMenuItemCategoryList;} @Override protected void onCreate(Bundle savedInstanceState) { getWindow().requestFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_home2); findViewById(R.id.tabs).bringToFront(); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Getting All Menu Items GetAllMenuItems menuItemsReader = null; try { menuItemsReader = new GetAllMenuItems(); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } allMenuItemsList = menuItemsReader.getMenuItemsList(); // Getting All Categories GetAllMenuItemCategories menuItemsCategoriesReader = null; try { menuItemsCategoriesReader = new GetAllMenuItemCategories(); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } allMenuItemCategoryList = menuItemsCategoriesReader.getCategoryList(); // toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //THIS!! if (viewPager != null) { viewPager.setCurrentItem(tab.getPosition()); } } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); tabLayout.setupWithViewPager(viewPager); setupTabIcons(); } private void setupTabIcons() { for (int i=0; i<allMenuItemCategoryList.size() ; i++){ TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tab.setText(allMenuItemCategoryList.get(i).Menu_Item_Category_Name); mappingOfCategoryWithIndex.add(i, allMenuItemCategoryList.get(i).Menu_Item_Category_Name) ; tabLayout.getTabAt(i).setCustomView(tab); } // int j = 0; // for (int i=0; i<allMenuItemCategoryList.size() ; i++){ // TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); // if(i%2==0){ // tab.setText(allMenuItemCategoryList.get(j).Menu_Item_Category_Name); // j++; // } // else{ // tab.setText(""); // // } // mappingOfCategoryWithIndex.add(i, allMenuItemCategoryList.get(i).Menu_Item_Category_Name) ; // tabLayout.getTabAt(i).setCustomView(tab); // // // //// tab.setCompoundDrawablesWithIntrinsicBounds(0, drawable.ic_launcher, 0, 0); // // // // } } private void setupViewPager(ViewPager viewPager) { adapter = new ViewPagerAdapter(getSupportFragmentManager()); for (int i=0 ; i<allMenuItemCategoryList.size() ; i++) { adapter.addFrag(new TabCategory(), allMenuItemCategoryList.get(i).Menu_Item_Category_Name); } // int j=0; // for (int i=0 ; i<allMenuItemCategoryList.size()*2 ; i++) { // if (i % 2 == 0) { // adapter.addFrag(new TabCategory(), allMenuItemCategoryList.get(j).Menu_Item_Category_Name); // j++; // } // else{ // adapter.addFrag(new dummyClass(), ""); // } // } viewPager.setAdapter(adapter); } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); // private final List<String> tabPositionCategory = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFrag(Fragment fragment, String title ) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } public class TabCategory extends Fragment{ private TextView nameOfItem; HomeActivity2 activity; public HomeActivity2.ViewPagerAdapter adapter; private TabLayout tabLayout; LinearLayout container1; public ArrayList<String> mappingOfCategoryWithIndex = new ArrayList<>(); public ArrayList<MenuItem> allMenuItemsList1 = new ArrayList<MenuItem>(); public ArrayList<MenuItemCategory> allCategoryList1 = new ArrayList<MenuItemCategory>(); private NonSwipeableViewPager viewPager; private String selectedCategory; private int selectedCategoryId; private ArrayList<MenuItem> menuItemsOfSelectedCategory = new ArrayList<>(); View rootView; public TabCategory() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activity = (HomeActivity2) getActivity(); allMenuItemsList1 = activity.getAllMenuItemsList(); mappingOfCategoryWithIndex =activity.getMappingOfCategoryWithIndex(); tabLayout = activity.getTabLayout(); viewPager = activity.getViewPager(); viewPager.clearOnPageChangeListeners(); adapter = activity.adapter; tabLayout.setupWithViewPager(viewPager); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment rootView = inflater.inflate(R.layout.tab_category, container, false); menuItemsOfSelectedCategory.removeAll(menuItemsOfSelectedCategory); menuItemsOfSelectedCategory.clear(); activity = (HomeActivity2) getActivity(); allMenuItemsList1 = activity.getAllMenuItemsList(); mappingOfCategoryWithIndex =activity.getMappingOfCategoryWithIndex(); tabLayout = activity.getTabLayout(); viewPager = activity.getViewPager(); adapter = activity.adapter; allCategoryList1 = activity.getAllMenuItemCategoryList(); int tab_position=tabLayout.getSelectedTabPosition(); selectedCategory = adapter.getPageTitle(tab_position).toString(); System.out.println("Tab Category: " + selectedCategory ); for (int i = 0 ; i<allMenuItemsList1.size();i++){ if(allMenuItemsList1.get(i).getCategoryName().equals(selectedCategory)){ menuItemsOfSelectedCategory.add(allMenuItemsList1.get(i)); } } System.out.println("Size of Category Items: " + menuItemsOfSelectedCategory.size() ); nameOfItem = (TextView) rootView.findViewById(R.id.nameofitem); System.out.println("Menu Size:"+ allMenuItemsList1.size()); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); container1 = (LinearLayout) rootView.findViewById(R.id.container1); container1.removeAllViews(); for (int i =0 ; i<menuItemsOfSelectedCategory.size();i++) { final View addView = layoutInflater.inflate(R.layout.menu_item, null); nameOfItem = (TextView) addView.findViewById(R.id.nameofitem); nameOfItem.setText(menuItemsOfSelectedCategory.get(i).getName()); container1.addView(addView); } } @Override public void onDestroyView() { super.onDestroyView(); // not cleaning up. menuItemsOfSelectedCategory.removeAll(menuItemsOfSelectedCategory); menuItemsOfSelectedCategory.clear(); container1.removeAllViews(); container1.invalidate(); System.out.println("destroyed!!!"); } }` 因为它们强制将checkbutton强制为单个状态。也就是说,我没有触及任何其他功能。

set check_HX 1