NameError:name' ttk'没有定义

时间:2018-05-15 16:22:10

标签: python nameerror ttk

尝试创建一个组合框,从2016年开始关注youtube教程,为我的学校工作制作一个货币计算器。

box = ttk.Combobox(LeftMainFrame, textvariable=value0, state='readonly',font=('arial', 20, 'bold'),width=20)
box['values'] = (' ', 'USA', 'Kenya', 'Brazil', 'Canada', 'India', 'Phillappines')
box.current(0)
box.grid(row=4, column=2)

一直说ttk没有定义。

我对python一无所知 我也在使用visual studio

1 个答案:

答案 0 :(得分:0)

来自python's ttk doc (python 2)same for python 3

  

要开始使用Ttk,请导入其模块:

#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
const int COLSIZE=3;
const int SIZE=15;
const int NAMESIZE=30;
void inputPersonalData(int, long[],char[][NAMESIZE],float[],float[]);
void calcBMI(int,float[],float[],float[]);
void bmiCategory(int,float[],char[][15]);
void displayPersonalData(int,long[],char[][NAMESIZE],float[],float[],float[],char[][15]);
void inputCal(int,char[][NAMESIZE],long[],float[][COLSIZE],char[][10]);
void totalCal(int,float[][COLSIZE],float[]);
void idealCal(int rowSize,float bmi[],float idealCal[]);
void condition(int,float [],float [],long [],char[][NAMESIZE]);
void searchId(int,long[],char[][NAMESIZE],float[],float[],float[],char[][15]);
int main()
{
    int numdata;
    long ID[SIZE];
    char name[SIZE][NAMESIZE];
    float weight[SIZE];
    float height[SIZE];
    float BMI[SIZE];
    char category[SIZE][15];
    char meal[COLSIZE][10];
    float calIntake[SIZE][COLSIZE];
    float totalCalIntake[SIZE];
    float idealCalIntake[SIZE];

    cout<<"Enter the number of models: ";
    cin>>numdata; //assume user input lower than size

    inputPersonalData(numdata,ID,name,weight,height);
    calcBMI(numdata,weight,height,BMI);
    bmiCategory(numdata,BMI,category);
    displayPersonalData(numdata,ID,name,weight,height,BMI,category);

    strcpy(meal[0],"BREAKFAST");
    strcpy(meal[1],"LUNCH");
    strcpy(meal[2],"DINNER");
    cout<<"\nDAILY CALORIE INTAKE for:\n";
    inputCal(numdata,name,ID,calIntake,meal);
    totalCal(numdata,calIntake,totalCalIntake);
    idealCal(numdata,BMI,idealCalIntake);
    condition(numdata,totalCalIntake,idealCalIntake,ID,name);
    searchId(numdata,ID,name,weight,height,BMI,category);

    return 0;
}
void inputPersonalData(int rowSize, long id[], char nama[][NAMESIZE],float berat[],float tinggi[])
{
    for(int i=0;i<rowSize;i++)
    {
        cout<<"\nModel "<<i+1<<"'s information\n\n";
        cout<<"ID: ";
        cin>>id[i];
        cout<<"Name: ";
        cin>>ws;
        cin.getline(nama[i],NAMESIZE);
        cout<<"Weight in kg: ";
        cin>>berat[i];
        cout<<"Height in m: ";
        cin>>tinggi[i];
        cout<<endl<<"***************************************"<<endl;
    }
}
void calcBMI(int rowSize,float berat[],float tinggi[],float bmi[])
{

    for(int j=0;j<rowSize;j++)
    {
        bmi[j]=(berat[j]/tinggi[j])/tinggi[j];
    }

}
void bmiCategory(int rowSize,float bmi[],char category[][15])
{
    for(int k=0;k<rowSize;k++)
    {
        if(bmi[k]>25.0)
            strcpy(category[k],"OVERWEIGHT");
        else if(bmi[k]>18.0)
            strcpy(category[k],"NORMAL");
        else
            strcpy(category[k],"UNDERWEIGHT");
    }
}
void displayPersonalData(int rowSize,long id[],char nama[][NAMESIZE],float berat[],float tinggi[],float bmi[],char category[][15])
{
	cout<<"\n****************************************************************************************************************\n";
    cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
    for(int m=0;m<rowSize;m++)
    {
        cout<<m+1<<"\t"<<id[m]<<"\t"<<nama[m]<<"\t\t"<<berat[m]<<"\t\t"<<tinggi[m]<<"\t\t"<<bmi[m]<<"\t\t"<<category[m]<<endl;
    }
}
void inputCal(int rowSize,char nama[][NAMESIZE],long id[],float calorie[][COLSIZE],char mealName[][10])
{
    for(int row=0;row<rowSize;row++)
    {
        cout<<"\nModel "<<row+1<<" ("<<nama[row]<<", ID: "<<id[row]<<") \n";
        for(int col=0;col<COLSIZE;col++)
        {
            cout<<"Calorie intake for "<<mealName[col]<<": ";
            cin>>calorie[row][col];
        }
    }
}
void totalCal(int rowSize,float calorie[][COLSIZE],float totalCalorie[])
{
	for(int p=0;p<rowSize;p++)
	{
		totalCalorie[p]=0;
		for(int q=0;q<COLSIZE;q++)
		{
			totalCalorie[p]=totalCalorie[p]+calorie[p][q];
		}
	}
}
void idealCal(int rowSize,float bmi[],float idealCal[])
{
	for(int n=0;n<rowSize;n++)
	{
		if(bmi[n]>25.0)
		{
			idealCal[n]=1500;
		}
		else if(bmi[n]>18.0)
		{
			idealCal[n]=2000;
		}
		else
		{
			idealCal[n]=2500;
		}
	}
}
void condition(int rowSize,float totalCalorie[],float idealCal[],long id[],char nama[][NAMESIZE])
{
	float minuteXercise[rowSize];
	for(int r=0;r<rowSize;r++)
	{
		if(totalCalorie[r]>idealCal[r])
		{
			minuteXercise[r]=(totalCalorie[r]-idealCal[r])/15.95;
			cout<<fixed<<setprecision(2);
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<") need to excercise for "<<minuteXercise[r]<<" minutes to burn excess calorie.\n";
		}
		else if(totalCalorie[r]<idealCal[r])
		{
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<") need to add "<<idealCal[r]-totalCalorie[r]<<" more calories to gain more weight.\n";
		}
		else
		{
			cout<<"\nModel "<<r+1<<" Id: "<<id[r]<<" ("<<nama[r]<<")'s calorie intake are perfect.\n";
		}
	}
}
void searchId(int rowSize,long id[],char nama[][NAMESIZE],float berat[],float tinggi[],float bmi[],char category[][15])
{
	long searchModel;
	bool foundId=false;
	cout<<"\nEnter ID to be search: ";
	cin>>searchModel;
	for(int s=0;s<rowSize;s++)
	{
		if(searchModel==id[s])
		{
			foundId=true;
			cout<<"\n****************************************************************************************************************\n";
    		cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
    		cout<<s+1<<"\t"<<id[s]<<"\t"<<nama[s]<<"\t\t"<<berat[s]<<"\t\t"<<tinggi[s]<<"\t\t"<<bmi[s]<<"\t\t"<<category[s]<<endl;
		}
	}
	while(!foundId)
	{
		cout<<"\nThe ID is not in our system!\n";
		cout<<"Enter ID to be search: ";
		cin>>searchModel;
		for(int s=0;s<rowSize;s++)
		{
			if(searchModel==id[s])
			{
				foundId=true;
				cout<<"\n****************************************************************************************************************\n";
   				cout<<"No.\tID\tName\t\t\tWeight(kg)\tHeight(m)\tBMI\t\tCategory\n";
   				cout<<s+1<<"\t"<<id[s]<<"\t"<<nama[s]<<"\t\t"<<berat[s]<<"\t\t"<<tinggi[s]<<"\t\t"<<bmi[s]<<"\t\t"<<category[s]<<endl;
			}
		}
	}
}
  

但是像这样的代码:

import ttk
  

可以选择使用它:

from Tkinter import *
  

然后几个ttk小部件(Button,Checkbutton,Entry,Frame,Label,LabelFrame,Menubutton,PanedWindow,Radiobutton,Scale和Scrollbar)将自动替换Tk小部件。

因此,根据您的环境,您可能需要导入ttk:

from Tkinter import *
from ttk import *